Bug #14974

"if" statement executes wrong branch

Added by kratob (Tobias Kraze) almost 2 years ago. Updated almost 2 years ago.

Target version:
ruby -v:
ruby 2.5.1p57 (2018-03-29 revision 63029) [x86_64-linux]


I've stumbled over a rather weird bug where Ruby 2.5+ executes an else branch of a condition when it should not. The best minimal example I could come up with is this:

def broken
  foo = 1
  if is_true or is_false
    foo = foo
    raise "FAIL"
  puts "PASS"

def is_true

def is_false


For me this script raises "FAIL" instead of printing "PASS".

Note that this is actually pretty much minimal; any of the following will "fix" the issue:

  • inlining is_true or is_false
  • removing the foo = foo or adding any other code to the if branch
  • replacing raise "FAIL" with puts "FAIL"
  • removing the puts "PASS"

Related issues

Related to Ruby master - Bug #14897: Unexpected behavior of `if` in specific codeClosedActions

Updated by jeremyevans0 (Jeremy Evans) almost 2 years ago

  • Status changed from Open to Closed

This appears to be a duplicate of #14897.

Updated by mame (Yusuke Endoh) almost 2 years ago

jeremyevans0 (Jeremy Evans) wrote:

This appears to be a duplicate of #14897.

Yes, it is already fixed in trunk. Please wait for the next release. Anyway, thank you for the report!

OT: I'm surprised that multiple persons encounter this issue because it occurs only when a nonsense assignment like foo = foo is used.


Updated by znz (Kazuhiro NISHIYAMA) almost 2 years ago

  • Related to Bug #14897: Unexpected behavior of `if` in specific code added

Updated by shevegen (Robert A. Heiler) almost 2 years ago

occurs only when a nonsense assignment like foo = foo is used.

Could happen because of a typo or the brain-associated with the
fingers in some thought process. I made very strange mistakes
myself; most common one I still make is "=" rather than "=="
simply because I somehow forget to type == (hit = twice). Happens
perhaps 1 in 1000 times only when I am very alert but when I
am quite sleepy it may be 1 in 100 times.

What I mean to say is that we can expect the unexpected as a
result of humans working with computers in general. :)

Also available in: Atom PDF