Monday, 19 August 2013

Operator precedence odd behaviour

Operator precedence odd behaviour

The reference I'm using for Ruby operators:
http://phrogz.net/programmingruby/language.html#table_18.4
According to this reference (and others I've seen), equality operators
take presendence over logical AND (&&, not Ruby's and).
So if we have the following:
foo = nil
foo < 5 # NoMethodError: undefined method `<' for nil:NilClass
# So to check for foo we do:
foo && (foo < 5) # Note the parenthesis
# But this works
foo && foo < 5 # why does this work??
In the last example foo && foo < 5, foo < 5 should happen first thus
resulting in an error before the AND can even be evaluated.
Am I missing something?

No comments:

Post a Comment