How false Is nil

Posted by root Mon, 04 Aug 2008 13:59:00 GMT

Use nil when the return is a boolean value.
Use false when the return is either false or not true object.
Otherwise they have pretty much the same semantic:
nil == false => false
nil.nil?        => true
false.nil?     => false
true.nil?      => false

if !nil && !false
  puts 'true'
end

if nil.nil? && !true.nil?
  puts 'true'
end

>> TrueClass.ancestors
=> [TrueClass, Object, Kernel]
>> FalseClass.ancestors
=> [FalseClass, Object, Kernel]
>> NilClass.ancestors
=> [NilClass, Object, Kernel]

Ruby core docs

Posted in  | Tags  | no comments | no trackbacks