Can it selfever evaluate to false or nil?
self
For instance...
class FooBar ... def check_this self && check_something_else end end
Is self &&this condition required ?
self &&
Yes. Remember that everything in Ruby is an object, therefore it selfis false for falseand nil:
false
nil
nil.instance_eval { self } # => nil false.instance_eval { self } # => false
Yes, but only in some very strange circumstances:
irb(main):001:0> def false.test irb(main):002:1> puts "hello" if !self irb(main):003:1> end => nil irb(main):004:0> false.test hello => nil
, self nil false .
nil false Ruby . self nil false :
def nil.self self end nil.self => nil def false.self self end false.self => false
, , , , .
Here is the corresponding article by Avdi Grimm on this issue, it seems impossible to fake the object, if only in very strange circumstances after NilClassor falseI suppose.
NilClass
class NilClass def selfie self end end nil.selfie #=> nil class FalseClass def selfie self end end false.selfie #=> false
This is actually impossible, because - if the object is zero or false - you cannot call a method on it because it does not belong to the class.