I have encountered a strange situation in the if condition of Ruby. Here is the sample code to reproduce the situation.
p1 = /hello/
p2 = /world/
s = "hello, world"
if m1 = s.match(p1) && m2 = s.match(p2)
puts "m1=#{m1}"
puts "m2=#{m2}"
end
The output will be
m1=world
m2=world
But I expected m1=hello because the && operator has higher precedence in Ruby operators.
This code
if m1 = s.match(p1) && m2 = s.match(p2)
seems to be interpreted as
if m1 = (s.match(p1) && m2 = s.match(p2))
Why is the logical AND operator && preceded over the assignment operator =?