Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Helper:
- # alternative `puts` with a non-nil return value
- # similar to many method calls which could be useful with logical operations
- def puts_with_true(arg)
- puts(arg)
- true
- end
- # puts returns `nil` by default
- puts 1 && 2
- 2 # printed
- #=> nil
- # `1 && 2` is evaluated first, then it's printed
- # read: puts (1 && 2)
- # puts 2
- puts 3 and 4
- 3 # printed
- #=> nil
- # `puts 3` is evaluated, returns `nil`
- # so `and 4` does not do anything
- # read: (puts 3) and 4
- # nil and 4
- # when first part of the expression returns a truthy value:
- puts_with_true 5 && 6
- 6 # printed
- #=> 6
- # `5 && 6` returns 6,
- # and this is printed and returned finally
- # read: puts_with_true (5 && 6)
- # puts_with_true 6
- puts_with_true 7 and 8
- 7 # printed
- #=> 8
- # `puts_with_true 7` returns true,
- # therefore `and 8` is evaluated and returns `8` as final value
- # read: (puts_with_true 7) and 8
- # true and 8
- # `and` has lower precedence than `&&`
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement