How is this possible? I thought Fixnum were ‘value’ type not real objects.
(in C they are VALUE which holds the value of the fixnum)
Almost everything in Ruby is an object – even Fixnums. There is only
one `3’ object – it’s not like a string, where two strings with the
same content can very well be different objects.
Almost everything in Ruby is an object – even Fixnums. There is only
one `3’ object – it’s not like a string, where two strings with the
same content can very well be different objects.
This is because Fixnums are immediates; so are symbols, btw.
Well, I found in the ruby sources, why this is possible.
Every ‘normal’ object has a hashtable for instance variables, this is
the
normal behaviour as we expect. But immediates don’t have a hashtable,
because they are encoded as values (bits) directly in the VALUE type.
To mimic the ‘object’ behaviour, Matz created a ‘generic_iv_tbl’ which
holds the instance variables of immediates for which a
‘instance_variable_set’
has been made. This is the magic.
This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.