# Fixnums can have instance variables? Cool

**URL:** <https://www.ruby-forum.com/t/fixnums-can-have-instance-variables-cool/82430>\
**Category:** Ruby\
**Created:** [December 30, 2005, 8:49am UTC](https://www.ruby-forum.com/t/fixnums-can-have-instance-variables-cool/82430 "2005-12-30T08:49:07Z")\
**Posts on this page:** 15\
**Page:** 1

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [December 30, 2005, 8:49am UTC](https://www.ruby-forum.com/t/fixnums-can-have-instance-variables-cool/82430/1 "2005-12-30T08:49:07Z")

</div>

This really surprises me:

3.instance\_variable\_set("@unbelievable", “believe it!”)

puts 3.instance\_variable\_get("@unbelievable") # -\> “believe it!”

which led me to:

class Fixnum  
attr\_accessor :note  
end

13.note = “a baker’s dozen”  
25.note = “5 squared”

notes = [13, 21, 25].collect { |x| x.note } # [“a baker’s dozen”,  
nil, “5 squared”]

Am I the only person with their jaw on the ground?

Gary W.

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [December 30, 2005, 9:40am UTC](https://www.ruby-forum.com/t/fixnums-can-have-instance-variables-cool/82430/2 "2005-12-30T09:40:44Z")

</div>

On 12/30/05, removed\_email\_address@domain.invalid [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> class Fixnum  
> attr\_accessor :note  
> end

> Am I the only person with their jaw on the ground?

that is very cool.

(‘a’…‘z’).each\_with\_index { |letter,index| index.note = letter }

That was my first little random hack, but I imagine there are more  
possibilities here 🙂

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [December 30, 2005, 9:59am UTC](https://www.ruby-forum.com/t/fixnums-can-have-instance-variables-cool/82430/3 "2005-12-30T09:59:44Z")

</div>

On 12/30/05, removed\_email\_address@domain.invalid [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Am I the only person with their jaw on the ground?

When I discovered this, what surprised me was not so much that you  
could do this, but that can do this _even\_though_ you can’t give them  
singleton methods (or access their singleton classes).

I just assumed the reason was because the singleton classes are stored  
in some structure that immediate values don’t have, but that instance  
variables are stored separately in some other way. (I never looked at  
the interpreter source to verify this, though.)

So the question in my mind: Is there a reason we couldn’t do that  
with ints, too?

Chris

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [December 30, 2005, 11:16am UTC](https://www.ruby-forum.com/t/fixnums-can-have-instance-variables-cool/82430/4 "2005-12-30T11:16:02Z")

</div>

class Object  
def foo  
“foo”  
end  
class \<\<self  
def bar  
“bar”  
end  
end  
end

examples:

“Some string”.foo  
-\> “foo”

Bignum.bar  
-\> "bar

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [December 30, 2005, 12:10pm UTC](https://www.ruby-forum.com/t/fixnums-can-have-instance-variables-cool/82430/5 "2005-12-30T12:10:26Z")

</div>

I’m pretty sure some of the crusty old rubyists here would say, Not  
being  
surprised about this is a first step towards ruby master… 🙂

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [December 30, 2005, 12:42pm UTC](https://www.ruby-forum.com/t/fixnums-can-have-instance-variables-cool/82430/6 "2005-12-30T12:42:27Z")

</div>

On 12/30/05, Pete [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> class Object  
> def foo  
> “foo”  
> end  
> class \<\<self  
> def bar  
> “bar”  
> end  
> end  
> end

No, I meant giving the Fixnums themselves singleton methods, like this:

irb(main):001:0\> a = ‘hello’  
=\> “hello”  
irb(main):002:0\> def a.hi  
irb(main):003:1\> ‘hi’  
irb(main):004:1\> end  
=\> nil  
irb(main):005:0\> a.hi  
=\> “hi”  
irb(main):006:0\> b = 5  
=\> 5  
irb(main):007:0\> def b.howdy  
irb(main):008:1\> ‘howdy’  
irb(main):009:1\> end  
TypeError: can’t define singleton method “howdy” for Fixnum  
from (irb):7  
irb(main):010:0\> ☹ Why not??

So I can do it for a string, but not a fixnum (or any other immediate  
object).

That’s what surprised me. You can have instance variables for 5, but  
not singleton methods for 5. (Of course you can have methods for 5,  
but if 5 responds to it, then 6 does, too.)

Cheers,

Chris

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [December 30, 2005, 4:40pm UTC](https://www.ruby-forum.com/t/fixnums-can-have-instance-variables-cool/82430/7 "2005-12-30T16:40:14Z")

</div>

Quoting [http://www.rubygarden.org/faq/entry/show/83](http://www.rubygarden.org/faq/entry/show/83) :

"Fixnums, Symbols, true, nil, and false are implemented as immediate  
values. With immediate values, variables hold the objects themselves,  
rather than references to them.

Singleton methods cannot be defined for such objects. Two Fixnums of  
the same value always represent the same object instance, so (for  
example) instance variables for the Fixnum with the value “one” are  
shared between all the “ones” is the system. This makes it impossible  
to define a singleton method for just one of these."

~ ryan ~

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [December 30, 2005, 10:12pm UTC](https://www.ruby-forum.com/t/fixnums-can-have-instance-variables-cool/82430/8 "2005-12-30T22:12:52Z")

</div>

On 12/30/05, J. Ryan S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Two Fixnums of  
> the same value always represent the same object instance, so (for  
> example) instance variables for the Fixnum with the value “one” are  
> shared between all the “ones” is the system.

Makes sense so far. Then we get this:

> This makes it impossible  
> to define a singleton method for just one of these."

What!? The implementation makes it impossible, not the fact that  
every 1 in the system refers to the same object. The constant  
“Object” refers to the same object (the Object class) every time I  
write it; this certainly doesn’t prevent one from writing singleton  
methods for it.

I’m not sure what the original author of that was trying to say, but I  
think the real answer is just that, for whatever implementation  
reasons (most likely for the sake of optimization), immediate objects  
can’t have singleton methods.

Again, my guess is that the choice was make to store singleton methods  
(or the pointer to the singleton class) in some “ruby object” struct  
that immediate values don’t have. But what I find surprising (going  
all the way back to the beginning 🙂 is that, given this, we can still  
define instance variables. The reason for this must be that instance  
variables are stored elsewhere (not in this “ruby object” struct, but  
in some other data store). But this seems an odd choice. I would  
have thought that both the klass pointer (as I think they call it) and  
the instance variables are stored in that struct. Maybe GC had  
something to do with this design… I can’t say more without looking  
at the source.

(Isn’t this usually where Guy steps in with an email consisting of 4  
words and the exact lines of the interpreter code? 🙂

Chris

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [December 30, 2005, 10:51pm UTC](https://www.ruby-forum.com/t/fixnums-can-have-instance-variables-cool/82430/9 "2005-12-30T22:51:59Z")

</div>

On Dec 30, 2005, at 4:10 PM, Chris P. wrote:

> I’m not sure what the original author of that was trying to say, but I  
> think the real answer is just that, for whatever implementation  
> reasons (most likely for the sake of optimization), immediate objects  
> can’t have singleton methods.

Without thinking too hard about it and without looking at the  
source code, I would guess that it is not supported to avoid  
the overhead of a method search for individual objects. You  
still have to do the method search for the class and as such  
you can extend the class but the search for a per/object  
method is avoided.

Gary W.

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [December 31, 2005, 11:14am UTC](https://www.ruby-forum.com/t/fixnums-can-have-instance-variables-cool/82430/10 "2005-12-31T11:14:39Z")

</div>

> > > > > “C” == Chris P. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) writes:

C\> (Isn’t this usually where Guy steps in with an email consisting of 4  
C\> words and the exact lines of the interpreter code? 🙂

[ruby-talk:17321]

🙂

Guy Decoux

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [December 31, 2005, 12:30pm UTC](https://www.ruby-forum.com/t/fixnums-can-have-instance-variables-cool/82430/11 "2005-12-31T12:30:19Z")

</div>

On Fri, 30 Dec 2005 07:46:17 -0000, [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> end
> 
> 13.note = “a baker’s dozen”  
> 25.note = “5 squared”
> 
> notes = [13, 21, 25].collect { |x| x.note } # [“a baker’s dozen”, nil,  
> “5 squared”]
> 
> Am I the only person with their jaw on the ground?

😃 That one got me, too. I kind of hinted at it in some nuby code I  
wrote  
([http://roscopeco.co.uk/code/noob/bitwise.html](http://roscopeco.co.uk/code/noob/bitwise.html)). Also check out:

irb(main):001:0\> class Symbol  
irb(main):002:1\> attr\_accessor :too  
irb(main):003:1\> end  
=\> nil  
irb(main):004:0\> :foo.too  
=\> nil  
irb(main):005:0\> :foo.too = 66  
=\> 66  
irb(main):006:0\> :foo.too  
=\> 66

This struck me as pretty cool, too:

irb(main):007:0\> SomethingElse = Symbol  
=\> Symbol  
irb(main):008:0\> :foo.is\_a? SomethingElse  
=\> true

God, Ruby is _good_

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [January 5, 2008, 4:07pm UTC](https://www.ruby-forum.com/t/fixnums-can-have-instance-variables-cool/82430/12 "2008-01-05T16:07:29Z")

</div>

> class Fixnum  
> attr\_accessor :note  
> end
> 
> 13.note = “a baker’s dozen”  
> 25.note = “5 squared”
> 
> notes = [13, 21, 25].collect { |x| x.note } # [“a baker’s dozen”,  
> nil, “5 squared”]

It’s really cool.

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [January 5, 2008, 4:33pm UTC](https://www.ruby-forum.com/t/fixnums-can-have-instance-variables-cool/82430/13 "2008-01-05T16:33:24Z")

</div>

On Jan 5, 2008, at 10:15 AM, Jason R. wrote:

> It’s one of the many things that makes Ruby code very fun to write  
> Actually it is a bit different. Monkey patching is just adding  
> methods to classes. Fixnum and String in your examples.

Instance variables on Fixnums (or Symbols or Nil) is different.  
It is a good example of uniformity in Ruby but I’m hard pressed  
to think of a nice use case for the feature.

Gary W.

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [January 5, 2008, 4:15pm UTC](https://www.ruby-forum.com/t/fixnums-can-have-instance-variables-cool/82430/14 "2008-01-05T16:15:47Z")

</div>

Welcome to Monkey Patching! This is how ActiveResource gives you things  
like:

4.days.ago

“some string”.underscore

and the like.

It’s one of the many things that makes Ruby code very fun to write.

Jason

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [January 5, 2008, 9:56pm UTC](https://www.ruby-forum.com/t/fixnums-can-have-instance-variables-cool/82430/15 "2008-01-05T21:56:00Z")

</div>

On Jan 5, 2008 4:32 PM, Gary W. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Actually it is a bit different. Monkey patching is just adding  
> methods to classes. Fixnum and String in your examples.
> 
> Instance variables on Fixnums (or Symbols or Nil) is different.  
> It is a good example of uniformity in Ruby but I’m hard pressed  
> to think of a nice use case for the feature.
> 
> Gary W.  
> You can use it to cache the result of method calls.

class Fixnum  
def factorial  
@factorial ||= self \* (self-1).factorial  
end  
end  
0.instance\_variable\_set(‘@factorial’,1)
