Well, as stated in the subject, what does '||=' mean?
It's from
def current_cart
if session[:cart_id] @current_cart ||= Cart.find(session[:cart_id]) #HERE!!!
session[:cart_id] = nil if @current_cart.purchased_at
end
if session[:cart_id].nil? @current_cart = Cart.create!
session[:cart_id] = @current_cart.id
end @current_cart
end
Is this Rails issue? I thought it's some Ruby grammar thing, because I
know there is an expression, '|='.
On Fri, Apr 20, 2012 at 9:15 PM, Soichi Ishida <lists@ruby-forum.com> wrote:
Well, as stated in the subject, what does '||=' mean?
It's from
def current_cart
if session[:cart_id] @current_cart ||= Cart.find(session[:cart_id]) #HERE!!!
session[:cart_id] = nil if @current_cart.purchased_at
end
if session[:cart_id].nil? @current_cart = Cart.create!
session[:cart_id] = @current_cart.id
end @current_cart
end
Is this Rails issue? I thought it's some Ruby grammar thing, because I
know there is an expression, '|='.
The best way of thinking of it, is that it will assign a default value
to a variable. So, if you have @var1 to initialize to a default value
when its not set already, then:
The info on that link is incorrect or outdated. On recent Rubies like 1.9.3
the nil guard explicitly does not show a warning if the variable did not exist
beforehand. Check it yourself:
On Fri, Apr 20, 2012 at 9:15 PM, Soichi Ishida <lists@ruby-forum.com> > wrote:
Well, as stated in the subject, what does '||=' mean?
It's from
def current_cart
if session[:cart_id] @current_cart ||= Cart.find(session[:cart_id]) #HERE!!!
session[:cart_id] = nil if @current_cart.purchased_at
end
if session[:cart_id].nil? @current_cart = Cart.create!
session[:cart_id] = @current_cart.id
end @current_cart
end
Is this Rails issue? I thought it's some Ruby grammar thing, because I
know there is an expression, '|='.
On Fri, Apr 20, 2012 at 9:15 PM, Soichi Ishida <lists@ruby-forum.com> wrote:
Well, as stated in the subject, what does '||=' mean?
It's from
def current_cart
if session[:cart_id] @current_cart ||= Cart.find(session[:cart_id]) #HERE!!!
session[:cart_id] = nil if @current_cart.purchased_at
end
if session[:cart_id].nil? @current_cart = Cart.create!
session[:cart_id] = @current_cart.id
end @current_cart
end
Is this Rails issue? I thought it's some Ruby grammar thing, because I
know there is an expression, '|='.
W dniu 21 kwietnia 2012 14:39 użytkownik Peter Zotov
<whitequark@whitequark.org> napisał:
The info on that link is incorrect or outdated. On recent Rubies like 1.9.3
the nil guard explicitly does not show a warning if the variable did not
exist
beforehand. Check it yourself:
On Fri, Apr 20, 2012 at 9:15 PM, Soichi Ishida <lists@ruby-forum.com >> <mailto:lists@ruby-forum.com>> wrote:
Well, as stated in the subject, what does '||=' mean?
It's from
def current_cart
if session[:cart_id] @current_cart ||= Cart.find(session[:cart_id]) #HERE!!!
session[:cart_id] = nil if @current_cart.purchased_at
end
if session[:cart_id].nil? @current_cart = Cart.create!
session[:cart_id] = @current_cart.id <http://current_cart.id/>
end @current_cart
end
Is this Rails issue? I thought it's some Ruby grammar thing, because I
know there is an expression, '|='.