0

print (-1 == -1) and (myobj.nil?)

true

print (-1 == -1) && (myobj.nil?)

false

Note, myobj.nil? returns false so, should not this always be false.

Shekhar
  • 67
  • 1
  • 7
  • 2
    Welcome to StackOverflow! In the future, please search the existing questions before asking your own. Chances are, it has already been asked, and, more importantly, answered. This way, we avoid duplication and keep all the answers neatly together in one place. Possible duplicates: http://StackOverflow.Com/questions/2083112/, http://StackOverflow.Com/questions/1625946/, http://StackOverflow.Com/questions/1426826/, http://StackOverflow.Com/questions/1840488/, http://StackOverflow.Com/questions/1434842/, http://StackOverflow.Com/questions/2376369/. – Jörg W Mittag May 14 '10 at 06:37
  • @Jörg: Searching for `and` and `&&` in Stack Overflow doesn't work, and entering the question title in "Ask Question" didn't reveal any duplicates. Stack overflow regulars ought to know that related questions exist, though. – Andrew Grimm May 16 '10 at 23:19

2 Answers2

3

&& and and have different precedence. In particular, and has very low precedence, lower than almost anything else.

The return value of print is nil, at least in Ruby 1.8. Therefore, the first expression reduces like this:

(print (-1 == -1)) and myobj.nil?
(print    true)    and myobj.nil? # => with the side-effect of printing "true"
      nil          and myobj.nil?
      nil

While the second expression reduces like this:

print ((-1 == -1) && myobj.nil?)
print (   true    && myobj.nil?)
print                myobj.nil?
print                 false       # => with the side-effect of printing "false"
            nil
Jörg W Mittag
  • 363,080
  • 75
  • 446
  • 653
1

Because they have different operator precedence.

The first evaluates as:

(print (-1 == -1)) and (myobj.nil?)

It prints true, because -1 == -1, then print returns nil and nil and false is nil.

The second is equivalent to:

print ((-1 == -1) && (myobj.nil?))

It prints (true && false), which is false, then print returns nil.

Matthew Flaschen
  • 278,309
  • 50
  • 514
  • 539
  • but why should that matter in this case, -1==-1 always evaluates to true, and myobj.nil? evaluates to false. so the net result should always be false. – Shekhar May 14 '10 at 06:36
  • oh ok, so if it was used in conditional expression like if (-1 == -1) and (myobj.nil?) both, would have same result? – Shekhar May 14 '10 at 06:42
  • 3
    @Shekhar: The return value of `print` is `nil`, at least in Ruby 1.8. Therefore, the first expression reduces like this: `(print (-1 == -1)) and myobj.nil?` → `(print true) and myobj.nil?` → `nil and myobj.nil?` (with the side-effect of printing **true**) → `nil`, while the second expression reduces like this: `print ((-1 == -1) && myobj.nil?)` → `print (true && myobj.nil?)` → `print myobj.nil?` → `print false` → `nil` (with the side-effect of printing **false**). – Jörg W Mittag May 14 '10 at 06:48
  • Jorg, thanks. this is really helpful, as I never realized print was being considered part of the expression. – Shekhar May 14 '10 at 07:10