Common Ruby Idioms
Posted
by DanSingerman
on Stack Overflow
See other posts from Stack Overflow
or by DanSingerman
Published on 2009-03-05T08:35:51Z
Indexed on
2010/03/28
9:43 UTC
Read the original article
Hit count: 503
One thing I love about ruby is that mostly it is a very readable language (which is great for self-documenting code)
However, inspired by this question: http://stackoverflow.com/questions/609612/ruby-code-explained
and the description of how ||=
works in ruby, I was thinking about the ruby idioms I don't use, as frankly, I don't fully grok them.
So my question is, similar to the example from the referenced question, what common, but not obvious, ruby idioms do I need to be aware of to be a truly proficient ruby programmer?
By the way, from the referenced question
a ||= b
is equivalent to
if a == nil || a == false
a = b
end
(Thanks to Ian Terrell for the correction)
Edit: It turns out this point is not totally uncontroversial. The correct expansion is in fact
(a || (a = (b)))
See these links for why:
- http://DABlog.RubyPAL.Com/2008/3/25/a-short-circuit-edge-case/
- http://DABlog.RubyPAL.Com/2008/3/26/short-circuit-post-correction/
- http://ProcNew.Com/ruby-short-circuit-edge-case-response.html
Thanks to Jörg W Mittag for pointing this out.
© Stack Overflow or respective owner