Hey all
I read in Agile Web D. with Rails that you can set a value like
this,
@my_time ||= Time.now
What does the operator mean, and what is the difference between using
||= and = ?
Hey all
I read in Agile Web D. with Rails that you can set a value like
this,
@my_time ||= Time.now
What does the operator mean, and what is the difference between using
||= and = ?
Nima wrote:
What does the operator mean, and what is the difference between using
||= and = ?
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!
Definitely does, thanks Chris!
I read in Agile Web D. with Rails that you can set a value like
this,
@my_time ||= Time.nowWhat does the operator mean, and what is the difference between using
||= and = ?
foo = 5 # foo now equals 5
foo ||= 6 # foo still equals 5
||= is saying if the left hand side is nil, then set the left hand side to
the right hand side, otherwise just leave well enough alone.
-philip
Hi Nima,
On 17 Jan 2007, at 10.16 pm, Nima wrote:
What does the operator mean, and what is the difference between using
||= and = ?
||= is called the conditional assignment. It is essentially syntactic
sugar for (i.e. a shorter way of writing):
@my_time = @my_time || Time.now
The way this works is based on the behaviour of the OR (||) part of
the assignment, and the general behaviour of statements in Ruby:
statements always return something.
So, let’s look at the following statement:
true || false
if you try this in irb you’ll see the following:
true || false
=> true
irb tells us that Ruby returns false for the expression true ||
false. If we remember our boolean logic, we know this is the right
answer.
However, where things start to get tricky is something like this:
true || “cheese”
=> true
false || “cheese”
=> “cheese”
“cheese” || “beef”
=> “cheese”
Notice how Ruby returns the value “cheese” in the second example
above, and the first value in the third example (Ruby shortcuts the
or, and knowing that true or anything is true, just returns the first
value).
Now, as Ruby isn’t statically typed, it lets us put anything on
either side of the || operator. Now, the way it turns a standard
object into true or false in order to evaluate the logic is as follows:
if the object is nil, or false, then it is represented as false.
if the object has any other value whatsoever, then it is represented
as true.
So, knowing this, we can see how Ruby sees the string “cheese” as
being true.
Now, returning back to the original question, we can see that:
@answer = false || “cheese”
would assign the value of “cheese” to answer.
Now, if we wrote:
@answer = @answer || “cheese”
then we get two possible behaviours:
Hope this helps,
Sam A.
This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.
Sponsor our Newsletter | Privacy Policy | Terms of Service | Remote Ruby Jobs