From: Sander Land Date: 2008-01-06T05:54:52+09:00 Subject: Re: Fixnums can have instance variables? Cool. On Jan 5, 2008 4:32 PM, Gary Wright 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 Wright 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)