Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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