Module.extend_object aFixnum

rubyists (we need something like ‘perl mongers’) -

can anyone confirm that it is not possible to call

1.extend aModule

and, if so, suggest a work around?

i have found several threads touching on this subject, but no definitive
answer. i find it odd that one can

class Object
def foo
‘bar’
end
end

1.foo

but not

module ObjectMethods
def foo
‘bar’
end
end

i = 1
i.extend ObjectMethods

which will give

TypeError: can’t define singleton

i understand that Fixnums are immeadiate, and thus may not ‘point’ to a symbol
table, but then how do the lookups into class Object’s symbol table work?

-ara

···

====================================

Ara Howard
NOAA Forecast Systems Laboratory
Information and Technology Services
Data Systems Group
R/FST 325 Broadway
Boulder, CO 80305-3328
Email: ahoward@fsl.noaa.gov
Phone: 303-497-7238
Fax: 303-497-7259
====================================

Hi –

rubyists (we need something like ‘perl mongers’) -

How about… “rubyists”? :slight_smile:

can anyone confirm that it is not possible to call

1.extend aModule

and, if so, suggest a work around?

Fixnums have no virtual class, so you can’t add singleton methods to
them.

irb(main):003:0> class << 1; end
TypeError: no virtual class for Fixnum
from (irb):3

I realize this is more of a further description than a real
explanation… but, well, maybe someone else can explain it further
:slight_smile:

i have found several threads touching on this subject, but no definitive
answer. i find it odd that one can

class Object
def foo
‘bar’
end
end

1.foo

but not

module ObjectMethods
def foo
‘bar’
end
end

i = 1
i.extend ObjectMethods

which will give

TypeError: can’t define singleton

Again, it’s because (I think) there’s no virtual class of 1, into
which the methods of ObjectMethods could be inserted. If you add a
method to Object (or Fixnum), then this isn’t an issue because the
search path for the method just goes to the (non-virtual) class.

David

···

On Wed, 16 Oct 2002, ahoward wrote:


David Alan Black
home: dblack@candle.superlink.net
work: blackdav@shu.edu
Web: http://pirate.shu.edu/~blackdav

Fixnums have no virtual class, so you can’t add singleton methods to
them.

my understanding (explained to me on this group) is that anytime an object is
extended an anonymous virtual class is created which would be at the bottom of
the inheritence tree, and thus the first one searched for methods.

Again, it’s because (I think) there’s no virtual class of 1, into
which the methods of ObjectMethods could be inserted. If you add a
method to Object (or Fixnum), then this isn’t an issue because the
search path for the method just goes to the (non-virtual) class.

so if the search path for 1 (or 42 or whatever) goes through Fixnum → Object,
it is possible to have multiple search paths - they are just not readily
modifiable? it seems like the search path for Fixnums is fixed for ALL
Fixnums then, and that what is needed is to modify the search path on a per
object level which is possible for non-immediate objects since these objects
point to something which can describe this search path while, in the case of
Fixnums, there is no such information and the search path is hard-wired into
the interpreter?

i’m not asking anything really - just trying to grok the situation…

-a

···

On Wed, 16 Oct 2002 dblack@candle.superlink.net wrote:

====================================

Ara Howard
NOAA Forecast Systems Laboratory
Information and Technology Services
Data Systems Group
R/FST 325 Broadway
Boulder, CO 80305-3328
Email: ahoward@fsl.noaa.gov
Phone: 303-497-7238
Fax: 303-497-7259
====================================