Hi --

On Wed, 16 Oct 2002, ahoward wrote:

>
>
> rubyists (we need something like 'perl mongers') -

How about... "rubyists"? :-)

> 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
:-)

> 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

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