I ran across the following and was just curious as to why this doesn’t work?
(Just in case you’re wondering why I’m asking all of these super-obscure
questions, I’m giving a presentation at our RUG tonight, and I just wanted
to make sure I knew… well, everything.)
irb(main):002:0> x = 5
=> 5
irb(main):003:0> def x.foo; ‘foo’; end
TypeError: can’t define singleton method “foo” for Fixnum
from (irb):3
As I understand it, the problem is that due to speed/convenience concerns,
fixnums have ‘immediate value’ and are not a reference to an object…
Maybe the following output from ‘ri’ will make more sense:
Fixnum objects have immediate value. This means that when they are
assigned or passed as parameters, the actual object is passed,
rather than a reference to that object. Assignment does not alias
Fixnum objects. There is effectively only one Fixnum object
instance for any given integer value, so, for example, you cannot
add a singleton method to a Fixnum.
Hope that helps-- // Bruce
···
On Monday 10 February 2003 05:26 pm, Chris Pine wrote:
Hello,
I ran across the following and was just curious as to why this doesn’t
work? (Just in case you’re wondering why I’m asking all of these
super-obscure questions, I’m giving a presentation at our RUG tonight, and
I just wanted to make sure I knew… well, everything.)
irb(main):002:0> x = 5
=> 5
irb(main):003:0> def x.foo; ‘foo’; end
TypeError: can’t define singleton method “foo” for Fixnum
from (irb):3