# Instance\_eval problem

**URL:** <https://www.ruby-forum.com/t/instance-eval-problem/129643>\
**Category:** Ruby\
**Created:** [February 20, 2008, 8:50am UTC](https://www.ruby-forum.com/t/instance-eval-problem/129643 "2008-02-20T08:50:42Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![bitmox](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/e95f7d/32.png) [@bitmox](https://www.ruby-forum.com/u/bitmox)\
**Post date:** [February 20, 2008, 8:50am UTC](https://www.ruby-forum.com/t/instance-eval-problem/129643/1 "2008-02-20T08:50:42Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![bitmox](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/e95f7d/32.png) [@bitmox](https://www.ruby-forum.com/u/bitmox)\
**Post date:** [February 20, 2008, 1:44pm UTC](https://www.ruby-forum.com/t/instance-eval-problem/129643/2 "2008-02-20T13:44:00Z")

</div>

Alle Wednesday 20 February 2008, bitmox ha scritto:

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

---

<div class="post-metadata">

**Author:** ![bitmox](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/e95f7d/32.png) [@bitmox](https://www.ruby-forum.com/u/bitmox)\
**Post date:** [February 20, 2008, 2:10pm UTC](https://www.ruby-forum.com/t/instance-eval-problem/129643/3 "2008-02-20T14:10:43Z")

</div>

On 20 Feb., 13:43, Stefano C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

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