From: "David A. Black" Date: 2008-05-02T01:56:21+09:00 Subject: Re: Please explain nuances of ||= Hi -- On Fri, 2 May 2008, Ruby Freak wrote: > I am reading some of the ruby files in rails and I an seeing the ||= > method used a lot. > knowing ruby the way I do, I realize that she has lots of magical > surprises and I really want to get to know this girl! x ||= y means: x || x = y The difference is that x ||= y won't complain if x is undefined, whereas if you type x || x = y and there's no x in scope, it will. The basic idea is that if x is (a) undefined (b) nil (c) false, you want to set it to y. Otherwise, you want to leave it alone. The whole expression evaluates to x, after the assignment has happened (or not). See http://dablog.rubypal.com/2008/3/25/a-short-circuit-edge-case for more. David -- Rails training from David A. Black and Ruby Power and Light: INTRO TO RAILS June 9-12 Berlin ADVANCING WITH RAILS June 16-19 Berlin INTRO TO RAILS June 24-27 London (Skills Matter) See http://www.rubypal.com for details and updates!