# Not able to understand the difference between "||=" and "|="

a = []
=> []

a ||=[]
=> []

a =[2,5,4]
=> [2, 5, 4]

a ||=[2,5,4]
=> [2, 5, 4]

a |=[2,5,4]
=> [2, 5, 4]

In the above code I am not able to understand the difference between
“||=” and “|=”.

Any help on this regard?

Huum, Interesting from the below code I got the taste :

a = [2,3,4]
=> [2, 3, 4]

a||=[2,1,4,6]
b ||= [2,33]
=> [2, 33]

b ||= [21,33]
=> [2, 33]

From the above code it is clear that “||=” operators set the variable
conditionally. Condition is like that if the variable is set to “false
or nil” then set it or return it’s already set value.

=> [2, 3, 4]

a |= [2,1,4,6]
=> [2, 3, 4, 1, 6]

a = [1,2,3]
=> [1, 2, 3]

a |= [4,5]
=> [1, 2, 3, 4, 5]

a |= [4,5,6]
=> [1, 2, 3, 4, 5, 6]

From the above code I can say that “|=” operator performing kind of
concatenation and if delicates then remove it away.

If any wrong logic I said here, forgive me and correct me please.

On the same road I tried to the “&&=” as below :

a = []
=> []

a &&= [4,1]
=> [4, 1]

a &&= [42,111]
=> [42, 111]

Couldn’t reach to any summary. How does it work?

ary | other_ary -> new_ary

Set Union—Returns a new array by joining this array with
other_ary, removing duplicates.

[ “a”, “b”, “c” ] | [ “c”, “d”, “a” ]
#=> [ “a”, “b”, “c”, “d” ]

Justin C. wrote in post #1098508:

On 02/22/2013 01:08 PM, Xavier R. wrote:

Couldn’t reach to any summary. How does it work?

Ruby Programming/Syntax/Operators - Wikibooks, open books for an open world

Guarded Assignment in Ruby

x &&= x.next_node #=> nil : x will be set to x.next_node, but only if x
is NOT nil or false.

then how the below works?

a = []
=> []
a &&= [2,3]
=> [2, 3]

On 02/22/2013 01:08 PM, Xavier R. wrote:

Couldn’t reach to any summary. How does it work?

https://en.wikibooks.org/wiki/Ruby_Programming/Syntax/Operators#1._Assignment

On 02/22/2013 01:32 PM, Xavier R. wrote:

Is `a` nil or false?

[] is not nil or false

if []
p “like true”
else
p “like false”
end

guess what this lines will output?

Justin C. wrote in post #1098511:

On 02/22/2013 01:32 PM, Xavier R. wrote:

Is `a` nil or false?

@justin not understood your point? yes the `a` is empty as we can see
below:

a = []
=> []

a.empty?
=> true

On Fri, Feb 22, 2013 at 11:51 PM, Xavier R. [email protected]
wrote:

guess what this lines will output?

got the point. Could you give me a valid example of “&&=” ?

I think I have something even better: advice how to find out yourself.
Fire up IRB and then start experimenting. It’s best to start from
simple expressions, so first for different combinations of a and b do

a && b

Look at the result. Then do

a &&= b
a ||= b

Reason about what you see. If unsure, do more tests.

Kind regards

robert

Hans M. wrote in post #1098517:

[] is not nil or false

if []
p “like true”
else
p “like false”
end

guess what this lines will output?

got the point. Could you give me a valid example of “&&=” ?

Justin C. wrote in post #1098508:

On 02/22/2013 01:08 PM, Xavier R. wrote:

Couldn’t reach to any summary. How does it work?

Thanks for the below link, all has been explained well.

Guarded Assignment in Ruby