Created
April 21, 2022 04:35
-
-
Save lucianghinda/2677e6549c9e5927bd88d9c646524564 to your computer and use it in GitHub Desktop.
What does ruby && operator return
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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