Nick Ginanto
Nick Ginanto

Reputation: 32130

Array difference work when not in variables

if given the following

a = [1,2,3]
b = [3,4,5]

a&b #=> [3]
b - a&b #=> [4,5]
b - a #=> [4,5]

why does this work

[1,2,3] - [3] #=> [1,2]

but not this

a - a&b #=> [] ??

Upvotes: 2

Views: 50

Answers (2)

pangpang
pangpang

Reputation: 8821

2.1.2 :006 > a - a&b
 => []
2.1.2 :007 > a - (a&b)
 => [1, 2]

You can get ruby operator precedence table from here.

Upvotes: 2

Marek Lipka
Marek Lipka

Reputation: 51151

Because - has higher precedence here than &:

a - (a&b)
# => [1, 2]

Upvotes: 5

Related Questions