So a variable can either be a Fixnum or a NilClass but not both.
In the context of a class instance method, self always refers to the instance, which is of the type of the class.
Also self cannot be changed:
irb(main):006:0> class C
irb(main):007:1> def z
irb(main):008:2> self = nil
irb(main):009:2> end
irb(main):014:1> end
SyntaxError: (irb):8: Can't change the value of self
self = nil
^
from D:/dev/Ruby20/bin/irb:12:in `<main>'
0
Grych
On
No. Object is either kind of Fixnum or NilClass. self is always a type of the current class.
0
August
On
Nope. The only way self could possibly equal nil is if you're inside NilClass.
class NilClass
def self_is_nil?
self == nil
end
end
nil.self_is_nil? # => true
This is impossible because
nilhas its own class,NilClass:So a variable can either be a
Fixnumor aNilClassbut not both.In the context of a class instance method,
selfalways refers to the instance, which is of the type of the class.Also
selfcannot be changed: