Christoph R.
12/1/2004 1:20:00 AM
itsme213 schrieb:
>irb(main):025:0> x = :any
>=> :any
>irb(main):026:0> def x.include(obj); true; end
>TypeError: can't define singleton method "include" for Symbol
> from (irb):26
>
>Why no singleton methods on Symbol?
>
>Are there other objects that are also not allowed to have singleton methods?
>
>
Yes you can't define singleton methods for Fixnums either - originally
the answers was all so called "immediate" objects (Fixnums, Symbols
nil,true, and false) which are Object existing outside of the garbage
collector but Matz implemented special singleton methods (+ singleton
class) for nil,true and false. For exmple, both
def nil.fine() end
class << true
Love = :forever
end
are okay.
Interestingly enough you can define instance variables
for all immediate values - for example
class Object
def you
p @you
end
end
:me.instance_variable_set(:@you, :you)
0.instance_variable_set(:@you, -1)
:me.you # :you
0.you # -1
/Christoph