As an expression

The value of an if is the value of the last expression found in each of its branches:

  1. a = if 2 > 1
  2. 3
  3. else
  4. 4
  5. end
  6. a #=> 3

If an if branch is empty, or it’s missing, it’s considered as if it had nil in it:

  1. if 1 > 2
  2. 3
  3. end
  4. # The above is the same as:
  5. if 1 > 2
  6. 3
  7. else
  8. nil
  9. end
  10. # Another example:
  11. if 1 > 2
  12. else
  13. 3
  14. end
  15. # The above is the same as:
  16. if 1 > 2
  17. nil
  18. else
  19. 3
  20. end