Hello,
It was always my understanding that immediate values could not have
singleton classes or instance variables because there is no full ruby object
associated with them: their VALUE is not a pointer to an object – it is
the object.
So I was quite surprised to find this:
$ irb
irb(main):001:0> 1.instance_eval do
irb(main):002:1* @var = 3
irb(main):003:1> end
3
irb(main):004:0> 1.instance_eval do
irb(main):005:1* @var
irb(main):006:1> end
3
irb(main):007:0> 2.instance_eval do
irb(main):008:1* @var
irb(main):009:1> end
nil
I thought this was impossible! Where is the instance variable stored? If
it’s possible to associate any amount of data I want to with the object `1’,
why can’t I associate a singleton class with it? Are instance variables
stored somewhere different from other ruby object internals (which immediate
values don’t have)? If so, what’s the reason for this? Speed?
It just seems odd that I can give `1’ an instance variable, but not a new
method.
Finally, exactly what things are and aren’t allowed with immediate objects?
Thanks for any help understanding,
Chris