# Instance variables for Fixnum?

**URL:** <https://www.ruby-forum.com/t/instance-variables-for-fixnum/60914>\
**Category:** Ruby\
**Created:** [May 18, 2006, 2:52pm UTC](https://www.ruby-forum.com/t/instance-variables-for-fixnum/60914 "2006-05-18T14:52:04Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![phi](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/bcef8e/32.png) [@phi](https://www.ruby-forum.com/u/phi)\
**Post date:** [May 18, 2006, 2:52pm UTC](https://www.ruby-forum.com/t/instance-variables-for-fixnum/60914/1 "2006-05-18T14:52:04Z")

</div>

Hi,

I’m studying ruby and tried this:

irb(main):001:0\> 1.instance\_variable\_set(:@foo, 123)  
=\> 123  
irb(main):002:0\> 2.instance\_variable\_get(:@foo)  
=\> nil  
irb(main):003:0\> 1.instance\_variable\_get(:@foo)  
=\> 123

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)

Thanx,

PHI

---

<div class="post-metadata">

**Author:** ![phi](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/bcef8e/32.png) [@phi](https://www.ruby-forum.com/u/phi)\
**Post date:** [May 18, 2006, 3:13pm UTC](https://www.ruby-forum.com/t/instance-variables-for-fixnum/60914/2 "2006-05-18T15:13:28Z")

</div>

Pascal H. wrote:

> irb(main):001:0\> 1.instance\_variable\_set(:@foo, 123)  
> =\> 123  
> irb(main):002:0\> 2.instance\_variable\_get(:@foo)  
> =\> nil  
> irb(main):003:0\> 1.instance\_variable\_get(:@foo)  
> =\> 123
> 
> 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.

“foo”.instance\_variable\_set(:@bar, “baz”)  
“foo”.instance\_variable\_get(:@bar) =\> ArgumentError

3.instance\_variable\_set(:@bar, “baz”)  
3.instance\_variable\_get(:@bar) =\> “baz”

This is because Fixnums are immediates; so are symbols, btw.

Daniel

---

<div class="post-metadata">

**Author:** ![phi](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/bcef8e/32.png) [@phi](https://www.ruby-forum.com/u/phi)\
**Post date:** [May 18, 2006, 3:59pm UTC](https://www.ruby-forum.com/t/instance-variables-for-fixnum/60914/3 "2006-05-18T15:59:13Z")

</div>

Daniel S. wrote:

> 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.
> 
> “foo”.instance\_variable\_set(:@bar, “baz”)  
> “foo”.instance\_variable\_get(:@bar) =\> ArgumentError

Yes, that’s ok

> 3.instance\_variable\_set(:@bar, “baz”)  
> 3.instance\_variable\_get(:@bar) =\> “baz”
> 
> 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.
