bitmox
2/20/2008 1:08:00 PM
On 20 Feb., 13:43, Stefano Crocco <stefano.cro...@alice.it> wrote:
> Alle Wednesday 20 February 2008, bitmox ha scritto:
>
>
>
> > Hi,
>
> > I' ve got problem with instance_eval.
>
> > I tried the following with irb:
> > >> obj = "3"
>
> > => "3"
>
> > >> obj.instance_eval "def a ; puts 'x'; end"
>
> > => nil
>
> > >> obj.a
>
> > x
> > => nil
>
> > >> obj = 3
>
> > => 3
>
> > >> obj.instance_eval "def a ; puts 'x'; end"
>
> > TypeError: (eval):1:in `irb_binding': no class/module to add method
> > from (irb):31
>
> > Why does the instance_eval not work for the Fixnum?
>
> > Best regards,
> > Thomas
>
Hi Stefano,
thank you for your hint.
I checked the documentation for Fixnum meanwhile. There is described,
that is impossible to a singleton method.
Thomas
> instance_eval works for Fixnums. For example:
>
> (-1).instance_eval("abs")
> => 1
>
> The problem is that you can't define singleton methods for Fixnums (this is
> also true for Floats and Bignums):
>
> x = 1
> def x.a
> end
> => TypeError: can't define singleton method "a" for Fixnum
>
> Stefano