Skip to content

Instantly share code, notes, and snippets.

@lucianghinda
Created April 21, 2022 04:35
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save lucianghinda/2677e6549c9e5927bd88d9c646524564 to your computer and use it in GitHub Desktop.
What does ruby && operator return
# nil and Truthy => nil
x = nil
y = Object.new
puts "when x=#{x.class} and y=#{y.class} then x && y returns #{(x && y).inspect} => x"
x = nil
y = true
puts "when x=#{x.class} and y=#{y.class} then x && y returns #{(x && y).inspect} => x"
# nil and falsy => nil
x = nil
y = false
puts "when x=#{x.class} and y=#{y.class} then x && y returns #{(x && y).inspect} => x"
x = nil
y = nil
puts "when x=#{x.class} and y=#{y.class} then x && y returns #{(x && y).inspect} => x"
# false and truthy => false
x = false
y = true
puts "when x=#{x.class} and y=#{y.class} then x && y returns #{(x && y).inspect} => x"
x = false
y = Object.new
puts "when x=#{x.class} and y=#{y.class} then x && y returns #{(x && y).inspect} => x"
# false and nil => false
x = false
y = nil
puts "when x=#{x.class} and y=#{y.class} then x && y returns #{(x && y).inspect} => x"
# true and falsy => falsy
x = true
y = nil
puts "when x=#{x.class} and y=#{y.class} then x && y returns #{(x && y).inspect} => y"
x = true
y = false
puts "when x=#{x.class} and y=#{y.class} then x && y returns #{(x && y).inspect} => y"
# truthy & truthy => last truthy
x = Array.new
y = Object.new
puts "when x=#{x.class} and y=#{y.class} then x && y returns #{(x && y).class} => y"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment