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?

···

--
Posted via http://www.ruby-forum.com/\.

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 via http://www.ruby-forum.com/\.

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 via http://www.ruby-forum.com/.

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 via http://www.ruby-forum.com/\.

[] is not nil or false

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

guess what this lines will output?

···

--
Posted via http://www.ruby-forum.com/.

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

···

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

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?

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 via http://www.ruby-forum.com/\.

Justin Collins wrote in post #1098508:

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

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

Guarded Assignment in Ruby

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]

···

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

--
Posted via http://www.ruby-forum.com/\.

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 Fri, Feb 22, 2013 at 11:51 PM, Xavier R. <lists@ruby-forum.com> wrote:

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 "&&=" ?

--
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/

Justin Collins wrote in post #1098508:

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

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

···

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

Guarded Assignment in Ruby

--
Posted via http://www.ruby-forum.com/\.

Is `a` nil or false?

···

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

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?

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

Guarded Assignment in Ruby

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]

Justin Collins wrote in post #1098511:

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

···

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

--
Posted via http://www.ruby-forum.com/\.