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
  • 29,034
  • 41
  • 131
  • 227

2 Answers2

5

Because - has higher precedence here than &:

a - (a&b)
# => [1, 2]
Marek Lipka
  • 49,740
  • 7
  • 83
  • 88
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,081
  • 10
  • 55
  • 93