2

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 #=> [] ??
Nick Ginanto
  • 31,090
  • 47
  • 134
  • 244

2 Answers2

5

Because - has higher precedence here than &:

a - (a&b)
# => [1, 2]
Marek Lipka
  • 50,622
  • 7
  • 87
  • 91
2
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.

Community
  • 1
  • 1
pangpang
  • 8,581
  • 11
  • 60
  • 96