Ruby Tricks
rubyinside.com
rubyinside.com
x = {:a => 1, :b => 2}
[* x]
=> [[:a, 1], [:b, 2]]
Some of the other tips just make the code less readable
I like how #1 is about 'taking the pain out of the process' where the 'pain' to be removed appears to be 'the method name that describes what your code is doing'.
I also learned that "Array#*, when supplied with a number, multiplies the size of the array by duplicate elements". I can only assume this is a very common and useful operation out there in GPU Vector Magic Land.
I was somewhat disappointed that #12 suggested using ranges for number comparison instead of the obviously more idiomatic solution of manipulating metaclasses so every number has a working 'decade' method.
It is certainly useful, albeit not exactly what I want. Then again, I said cursory.
>> 3. Format decimals
>> money = 9.5
>> "%.2f" % money # => "9.50"
Seriously? IMHO These sorts of things are not nice. To read the code you have to know these special magic operators. It's not immediately obvious it's calling a function to do string formatting.Readable code is so important :/
I love using printf all the time in my programs and it is one of my litmus tests for a new language - does it have easy quick access to sprintf? (Java failed this test)