From: "Ian M. Asaff" Date: 2012-04-21T10:20:57+09:00 Subject: Re: What does '||=' mean? --bcaec54c546e759cd104be2637a7 Content-Type: text/plain; charset=ISO-8859-1 equivalent to @ivar = @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.pragprog.com/forums/130/topics/3644 On Fri, Apr 20, 2012 at 9:15 PM, Soichi Ishida 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, '|='. > > Thanks in advance. > > > soichi > > -- > Posted via http://www.ruby-forum.com/. > > --bcaec54c546e759cd104be2637a7 Content-Type: text/html; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable equivalent to=A0

@ivar =3D @ivar || value

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

see=A0http://forums.pragprog.com/forums/130/topics/3644


--bcaec54c546e759cd104be2637a7--