Possible Duplicate:
What does ||= (or equals) mean in Ruby?
RoR: Meaning of “user ||= User.new”
I saw it used in this Railscast:
@current_user ||= User.find(session[:user_id]) if session[:user_id]
Possible Duplicate:
What does ||= (or equals) mean in Ruby?
RoR: Meaning of “user ||= User.new”
I saw it used in this Railscast:
@current_user ||= User.find(session[:user_id]) if session[:user_id]
in Ruby, we can write the following code to operate itself.
x += 1
this means equally
x = x + 1
In intialize process, we want set initial value to a variable only when it is nil or not exist.
For example,
a = a || initial_value
First, the left condition is evaluated. If a is evaluated as false, the right condition is evaluated and a is assigned initial_value.
And, we can rewrite
a = a || initial_value
as the following
a ||= initial_value