[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Justin To

6/11/2008 9:00:00 PM

what does ||= do?
--
Posted via http://www.ruby-....

10 Answers

Todd Benson

6/11/2008 9:09:00 PM

0

On Wed, Jun 11, 2008 at 3:59 PM, Justin To <tekmc@hotmail.com> wrote:
> what does ||= do?

irb is your friend :-)

Todd

Martin DeMello

6/11/2008 9:12:00 PM

0

On Wed, Jun 11, 2008 at 1:59 PM, Justin To <tekmc@hotmail.com> wrote:
> what does ||= do?

a ||= b is shorthand for a = a || b

|| is the "or" operator - a || b evaluates to a unless a is nil or
false, in which case it evaluates to b

a ||= b is most commonly used to say "if a hasn't already been set
(and is therefore nil), set it to b, else leave it alone"

martin

Lyle Johnson

6/11/2008 9:12:00 PM

0

On Wed, Jun 11, 2008 at 3:59 PM, Justin To <tekmc@hotmail.com> wrote:

> what does ||= do?

The expression:

x ||= y

is shorthand for:

x = x || y

where "||" is the OR-operator. If x evaluates as false or nil, the
value of y will be assigned to x; otherwise, x keeps its original
value. So, for example,

x = nil
x ||= 42 # now x has value 42
x ||= 23 # x stays at 42

Hope this helps,

Lyle

Rob Biedenharn

6/11/2008 9:21:00 PM

0

On Jun 11, 2008, at 5:12 PM, Martin DeMello wrote:

> On Wed, Jun 11, 2008 at 1:59 PM, Justin To <tekmc@hotmail.com> wrote:
>> what does ||= do?
>
> a ||= b is shorthand for a = a || b
>
> || is the "or" operator - a || b evaluates to a unless a is nil or
> false, in which case it evaluates to b
>
> a ||= b is most commonly used to say "if a hasn't already been set
> (and is therefore nil), set it to b, else leave it alone"
>
> martin


Just to short-circuit the discussion:

a ||= b

is actually implemented as if:

a || a=b

Search for recent threads or find David A. Black's blog.

-Rob

Rob Biedenharn http://agileconsult...
Rob@AgileConsultingLLC.com



Rob Biedenharn

6/11/2008 9:24:00 PM

0


On Jun 11, 2008, at 5:12 PM, Lyle Johnson wrote:

> On Wed, Jun 11, 2008 at 3:59 PM, Justin To <tekmc@hotmail.com> wrote:
>
>> what does ||= do?
>
> The expression:
>
> x ||= y
>
> is shorthand for:
>
> x = x || y
>
> where "||" is the OR-operator. If x evaluates as false or nil, the
> value of y will be assigned to x; otherwise, x keeps its original
> value. So, for example,
>
> x = nil
> x ||= 42 # now x has value 42
> x ||= 23 # x stays at 42
>
> Hope this helps,
>
> Lyle
>


Actually, it is like:

x || x=y

Refer to:
http://dablog.rubypal.com/2008/3/25/a-short-circuit...

-Rob

Todd Benson

6/11/2008 9:24:00 PM

0

On Wed, Jun 11, 2008 at 4:12 PM, Lyle Johnson <lyle@lylejohnson.name> wrote:
> On Wed, Jun 11, 2008 at 3:59 PM, Justin To <tekmc@hotmail.com> wrote:
>
>> what does ||= do?
>
> The expression:
>
> x ||= y
>
> is shorthand for:
>
> x = x || y
>
> where "||" is the OR-operator. If x evaluates as false or nil, the
> value of y will be assigned to x; otherwise, x keeps its original
> value. So, for example,
>
> x = nil
> x ||= 42 # now x has value 42
> x ||= 23 # x stays at 42

Not exactly.

a = 2
a ||= b #where b is not defined
p a #we get 2
p b #we get NameError

Not only that, but there are other subtleties that many gurus on this
list have pointed out.

Todd

Damjan Rems

6/12/2008 5:58:00 AM

0

Justin To wrote:
> what does ||= do?

In other languages you would code this something like:

if a == nil
a = b
end


by
TheR
--
Posted via http://www.ruby-....

msnews.microsoft.com

6/12/2008 10:06:00 PM

0

careful..

this is really...

if not a
a = b
end

(it will assign b if a resolves to false as a boolean (i.e. either nil
or false)


On Jun 12, 2008, at 1:57 AM, Damjan Rems wrote:

> Justin To wrote:
>> what does ||= do?
>
> In other languages you would code this something like:
>
> if a == nil
> a = b
> end
>
>
> by
> TheR
> --
> Posted via http://www.ruby-....
>


msnews.microsoft.com

6/12/2008 10:08:00 PM

0

(need to finish replies before clicking "send")

this really is equivalent to a = a || b, just like a += 1 is
equivalent to a = a + 1 (same for -=, etc.)

that's why you get b if a was false

On Jun 12, 2008, at 1:57 AM, Damjan Rems wrote:

> Justin To wrote:
>> what does ||= do?
>
> In other languages you would code this something like:
>
> if a == nil
> a = b
> end
>
>
> by
> TheR
> --
> Posted via http://www.ruby-....
>


Mikael Høilund

6/12/2008 10:26:00 PM

0

On Jun 13, 2008, at 0:08, Mike Cargal wrote:
> this really is equivalent to a =3D a || b, just like a +=3D 1 is =20
> equivalent to a =3D a + 1 (same for -=3D, etc.

However, be aware that this particular operator has an optimization =20
making it behave like

a || a =3D b

instead. Example problem:

>> h =3D Hash.new() { "default" } # Makes a Hash with "default" as the =20=

default value instead of nil
=3D> {}
>> h[:a]
=3D> "default"
>> h.has_key? :a
=3D> false

>> h[:a] ||=3D "not default"
=3D> "default"
>> h[:a]
=3D> "default"
>> h.has_key? :a
=3D> false

>> h[:a] =3D h[:a] || "not default"
=3D> "default"
>> h[:a]
=3D> "default"
>> h.has_key? :a
=3D> true
>>

--=20
# Mikael H=F8ilund
def method_missing(m, a=3D0) a +
m.to_s[/[a-z]+/].size * 2; end
p What is the meaning of life?