# Forum: Ruby Not able to understand the difference between "||=" and "|=".

on 2013-02-22 21:20
```>> 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?```
on 2013-02-22 21:45
```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 2013-02-22 21:47
```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" ]```
on 2013-02-22 22:08
```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?```
on 2013-02-22 22:25
```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/Syn...

http://lesseverything.com/blog/archives/2008/12/16...```
on 2013-02-22 22:32
```Justin Collins wrote in post #1098508:
> 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/Syn...
>
> http://lesseverything.com/blog/archives/2008/12/16...

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 2013-02-22 22:45
```On 02/22/2013 01:32 PM, Xavier R. wrote:
>>>
>

Is `a` nil or false?```
on 2013-02-22 23:19
```Justin Collins 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 2013-02-22 23:49
```[] is not nil or false

if []
p "like true"
else
p "like false"
end

guess what this lines will output?```
on 2013-02-22 23:51
```Hans Mackowiak 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 "&&=" ?```
on 2013-02-23 10:46
```On Fri, Feb 22, 2013 at 11:51 PM, Xavier R. <lists@ruby-forum.com>
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```
on 2013-02-23 11:24
```Justin Collins 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.

>
> http://lesseverything.com/blog/archives/2008/12/16...```