From: Florian Gilcher Date: 2012-04-22T00:44:22+09:00 Subject: Re: What does '||=' mean? --Apple-Mail-1-233554399 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset=us-ascii Close. a ||=3D b=20 Is actually equivalent to a || a =3D b This is not as obvious in the ivar case, but combined with [] access = like hashes, it is a bit different: foo[:bar] || foo[:bar] =3D 'batz' [] is called once if :bar is present, and []=3D is called once if :bar = is not present. While in: foo[:bar] =3D foo[:bar] || 'batz' both [] and []=3D are called, independent of the value if :bar. On Apr 21, 2012, at 3:20 AM, Ian M. Asaff wrote: > equivalent to=20 >=20 > @ivar =3D @ivar || value >=20 > ruby || short circuits, so if @ivar is nil, then value gets assigned = to @ivar. it's referred to as a "nil guard".=20 >=20 > see http://forums.pragprog.com/forums/130/topics/3644 >=20 > On Fri, Apr 20, 2012 at 9:15 PM, Soichi Ishida = wrote: > Well, as stated in the subject, what does '||=3D' mean? >=20 > It's from >=20 > def current_cart > if session[:cart_id] > @current_cart ||=3D Cart.find(session[:cart_id]) #HERE!!! > session[:cart_id] =3D nil if @current_cart.purchased_at > end > if session[:cart_id].nil? > @current_cart =3D Cart.create! > session[:cart_id] =3D @current_cart.id > end > @current_cart > end >=20 > Is this Rails issue? I thought it's some Ruby grammar thing, because I > know there is an expression, '|=3D'. >=20 > Thanks in advance. >=20 >=20 > soichi >=20 > -- > Posted via http://www.ruby-forum.com/. >=20 >=20 -- Florian Gilcher smtp: flo@andersground.net jabber: Skade@jabber.ccc.de gpg: 533148E2 --Apple-Mail-1-233554399 Content-Transfer-Encoding: quoted-printable Content-Type: text/html; charset=us-ascii
equivalent = to 

@ivar =3D @ivar || = value

ruby || short circuits, so if @ivar is = nil, then value gets assigned to @ivar. it's referred to as a "nil = guard". 

see http://forums.p= ragprog.com/forums/130/topics/3644

On Fri, Apr 20, 2012 at 9:15 PM, Soichi Ishida = <lists@ruby-forum.com> = wrote:
Well, as stated in the = subject, what does '||=3D' mean?

It's from

 def current_cart
   if session[:cart_id]
     @current_cart ||=3D Cart.find(session[:cart_id]) =  #HERE!!!
     session[:cart_id] =3D nil if = @current_cart.purchased_at
   end
   if session[:cart_id].nil?
     @current_cart =3D Cart.create!
     session[:cart_id] =3D @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, '|=3D'.

Thanks in advance.


soichi

--
Posted via http://www.ruby-forum.com/.



--
Florian = Gilcher

gpg:=    533148E2

= --Apple-Mail-1-233554399--