true, false, and nil
Ruby has three tiny but important values: true, false, and
nil. Each is an instance of its own class:
$ irb
>> true.class
=> TrueClass
>> false.class
=> FalseClass
>> nil.class
=> NilClass
>> exit
true and false are Ruby’s booleans. nil represents the
absence of a value.
The only values that Ruby treats as falsy are false and nil.
Everything else — including 0, "", and [] — is treated as
truthy. This is an important rule and a frequent source of
surprises for newcomers coming from other languages.
$ irb
>> if 0
?> puts '0 is truthy'
>> end
0 is truthy
=> nil
>> if ''
?> puts 'empty string is truthy'
>> end
empty string is truthy
=> nil
>> exit
|
The word |