From: Stefano Crocco Date: 2008-02-20T21:43:09+09:00 Subject: Re: instance_eval problem 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 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