Justin Ko wrote:
> I'm looking at this method and don't understand what ||= means:
Much like 'x += 1' is a shortcut for 'x = x + 1', 'x ||= 1' is a
shortcut for 'x = x || 1'.
The significance of this is that the || method doesn't simply return a
boolean value; it checks the first argument, returns it if it evaluates
to 'true' (i.e. is something other than false or nil), and otherwise
returns the second argument. So it's commonly used as a shortcut for an
if/then statement, checking if the first argument is nil (or false). So
the following are all equivalent:
if @account
@account
else
Account.find(session[:account_id])
end
is the same as:
@account = @account || Account.find(session[:account_id])
is the same as:
@account ||= Account.find(session[:account_id])
Hope this helps!
--
Posted via http://www.ruby-forum.com/.