What does &&= do?

S

Subbu

I came across &&= while reading some code. I know what ||= does but
not this one. Can someone explain what it does?

Thanks much
-subbu
 
J

Jano Svitok

I came across &&= while reading some code. I know what ||= does but
not this one. Can someone explain what it does?

Thanks much
-subbu

a &&=b is a shortcut for a = a && b
 
R

R.F. Pels

Subbu said:
I came across &&= while reading some code. I know what ||= does but
not this one. Can someone explain what it does?

self assignment
Examples:
foo += 12

Syntax:
expr op= expr # left hand side must be assignable.

This form evaluated as expr = expr op expr. But right hand side expression
evaluated once. op can be one of:
+, -, *, /, %, **, &, |, ^, <<, >>, &&, ||

There may be no space between operators and =. >

RTFM?
 
C

Chad Perrin

I came across &&= while reading some code. I know what ||= does but
not this one. Can someone explain what it does?

The other answers are not wrong, but they may not be clear.

Just as ||= sets the value of something if it doesn't already have one,
&&= sets the value of something if it *does* already have one.

irb(main):001:0> a = nil
=> nil
irb(main):002:0> b = 'foo'
=> "foo"
irb(main):003:0> a &&= b
=> nil
irb(main):004:0> a = 'foo'
=> "foo"
irb(main):005:0> b = 'bar'
=> "bar"
irb(main):006:0> a &&= b
=> "bar"

Is that clear enough?
 
R

Rick DeNatale

The other answers are not wrong, but they may not be clear.

Just as ||= sets the value of something if it doesn't already have one,
&&= sets the value of something if it *does* already have one.

irb(main):001:0> a = nil
=> nil
irb(main):002:0> b = 'foo'
=> "foo"
irb(main):003:0> a &&= b
=> nil
irb(main):004:0> a = 'foo'
=> "foo"
irb(main):005:0> b = 'bar'
=> "bar"
irb(main):006:0> a &&= b
=> "bar"

Almost, but not quite:

irb(main):001:0> a = false
=> false
irb(main):002:0> b = 'foo'
=> "foo"
irb(main):003:0> a &&= b
=> false
irb(main):004:0> a
=> false
irb(main):005:0> a ||= b
=> "foo"
irb(main):006:0> a
=> "foo"
irb(main):007:0>

||= sets the value of a variable if its value if nil or false,
&&= sets the value of something if it its value is something other
than nil or false.
 
C

Chad Perrin

Almost, but not quite:

irb(main):001:0> a = false
=> false
irb(main):002:0> b = 'foo'
=> "foo"
irb(main):003:0> a &&= b
=> false
irb(main):004:0> a
=> false
irb(main):005:0> a ||= b
=> "foo"
irb(main):006:0> a
=> "foo"
irb(main):007:0>

||= sets the value of a variable if its value if nil or false,
&&= sets the value of something if it its value is something other
than nil or false.

Technically more correct than my answer, but mine wasn't *incorrect*. It
just left out the "or false" bit. Generally, ||= and &&= are used in
cases where the variable's value may be nil, in my experience, and I
forgot to consider the rest of the story.
 
K

Ken Bloom

The other answers are not wrong, but they may not be clear.

Just as ||= sets the value of something if it doesn't already have one,
&&= sets the value of something if it *does* already have one.

irb(main):001:0> a = nil
=> nil
irb(main):002:0> b = 'foo'
=> "foo"
irb(main):003:0> a &&= b
=> nil
irb(main):004:0> a = 'foo'
=> "foo"
irb(main):005:0> b = 'bar'
=> "bar"
irb(main):006:0> a &&= b
=> "bar"

Is that clear enough?

Consider the following practical use I gave in my solution for Ruby Quiz
144:

DAYNAMES=%w[Sun Mon Tue Wed Thu Fri Sat]
DAYNAME=%r{Sun|Mon|Tue|Wed|Thu|Fri|Sat}
TIME=%r{[0-9]+}

#find a single day, or a hyphenated range of days
clause.scan(/(#{DAYNAME})(?:(?=\s)|$)|(#{DAYNAME})-(#{DAYNAME})/) \
do |single,start,finish|

#convert data in single, start, and finish only if they're not nil
#though the &&= is not strictly necessary since Array#index() handles
#nil properly, some conversions like obj.to_i will fail badly
#if obj is nil
single &&= DAYNAMES.index(single)
start &&= DAYNAMES.index(start)
finish &&= DAYNAMES.index(finish)

#do something with the ones that are not nil

end
 
S

Subbu

The other answers are not wrong, but they may not be clear.
Just as ||= sets the value of something if it doesn't already have one,
&&= sets the value of something if it *does* already have one.
irb(main):001:0> a = nil
=> nil
irb(main):002:0> b = 'foo'
=> "foo"
irb(main):003:0> a&&= b
=> nil
irb(main):004:0> a = 'foo'
=> "foo"
irb(main):005:0> b = 'bar'
=> "bar"
irb(main):006:0> a&&= b
=> "bar"
Is that clear enough?

Consider the following practical use I gave in my solution for Ruby Quiz
144:

DAYNAMES=%w[Sun Mon Tue Wed Thu Fri Sat]
DAYNAME=%r{Sun|Mon|Tue|Wed|Thu|Fri|Sat}
TIME=%r{[0-9]+}

#find a single day, or a hyphenated range of days
clause.scan(/(#{DAYNAME})(?:(?=\s)|$)|(#{DAYNAME})-(#{DAYNAME})/) \
do |single,start,finish|

#convert data in single, start, and finish only if they're not nil
#though the&&= is not strictly necessary since Array#index() handles
#nil properly, some conversions like obj.to_i will fail badly
#if obj is nil
single&&= DAYNAMES.index(single)
start&&= DAYNAMES.index(start)
finish&&= DAYNAMES.index(finish)

#do something with the ones that are not nil

end

Thanks all for the detailed explanations. Really nice of you.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,285
Messages
2,571,416
Members
48,111
Latest member
PorterZ31

Latest Threads

Top