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

Posted by Xavier R. (arup_r)
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?
Posted by Xavier R. (arup_r)
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.
Posted by Hans Mackowiak (hanmac)
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" ]
Posted by Xavier R. (arup_r)
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?
Posted by Justin Collins (Guest)
on 2013-02-22 22:25
(Received via mailing list)
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...
Posted by Xavier R. (arup_r)
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...


from your reference -

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]
>>
Posted by Justin Collins (Guest)
on 2013-02-22 22:45
(Received via mailing list)
On 02/22/2013 01:32 PM, Xavier R. wrote:
> from your reference -
>>>
>


Is `a` nil or false?
Posted by Xavier R. (arup_r)
on 2013-02-22 23:19
Justin Collins wrote in post #1098511:
> On 02/22/2013 01:32 PM, Xavier R. wrote:
>> from your reference -
>>>>
>>
>
>
> Is `a` nil or false?

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


>> a = []
=> []
>> a.empty?
=> true
>>
Posted by Hans Mackowiak (hanmac)
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?
Posted by Xavier R. (arup_r)
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 "&&=" ?
Posted by Robert Klemme (robert_k78)
on 2013-02-23 10:46
(Received via mailing list)
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
Posted by Love U Ruby (my-ruby)
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...
Please log in before posting. Registration is free and takes only a minute.
Existing account (Switch to SSL-encrypted connection)
NEW: Do you have a Google/GoogleMail or Yahoo account? No registration required!
Log in with Google account | Log in with Yahoo account
No account? Register here.