This code snippet creates the output followed below...
#! /usr/bin/env ruby
# Clemens Lee, 2013-10-15
puts
puts "In Ruby, what is true, and what is not."
candidates = [ true, false, Object.new, nil, 1, 0, 1.0, 0.0, "string", "" ]
puts
candidates.each do |x|
puts "#{x.inspect} is #{x == true}"
puts "#{x.inspect} is #{!(x == false)}"
end
puts
candidates.each do |x|
true_or_false = false
if x
true_or_false = true
end
puts "if #{x.inspect} is #{true_or_false}"
end
...
In Ruby, what is true, and what is not.
true is true
true is true
false is false
false is false
#<Object:0x0000010108ab90> is false
#<Object:0x0000010108ab90> is true
nil is false
nil is true
1 is false
1 is true
0 is false
0 is true
1.0 is false
1.0 is true
0.0 is false
0.0 is true
"string" is false
"string" is true
"" is false
"" is true
if true is true
if false is false
if #<Object:0x0000010108ab90> is true
if nil is false
if 1 is true
if 0 is true
if 1.0 is true
if 0.0 is true
if "string" is true
if "" is true
No comments:
Post a Comment