# \[Bug #2787\] nil.instance\_eval pushes nil onto cref

**URL:** <https://www.ruby-forum.com/t/bug-2787-nil-instance-eval-pushes-nil-onto-cref/184032>\
**Category:** Development\
**Created:** [February 24, 2010, 1:54am UTC](https://www.ruby-forum.com/t/bug-2787-nil-instance-eval-pushes-nil-onto-cref/184032 "2010-02-24T01:54:08Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Shugo\_M](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/7ea924/32.png) [@Shugo\_M](https://www.ruby-forum.com/u/Shugo_M)\
**Post date:** [February 24, 2010, 1:54am UTC](https://www.ruby-forum.com/t/bug-2787-nil-instance-eval-pushes-nil-onto-cref/184032/1 "2010-02-24T01:54:08Z")

</div>

Bug #2787: nil.instance\_eval pushes nil onto cref  
[http://redmine.ruby-lang.org/issues/show/2787](http://redmine.ruby-lang.org/issues/show/2787)

e$B5/I\<\<Te(B: Shugo M.  
e$B%9%F!\<%?%9e(B: Assigned, e$BM%@hEYe(B: Low  
e$BC4Ev\<Te(B: Yukihiro M., e$B%+%F%4%je(B: core  
ruby -v: ruby 1.9.2dev (2010-02-18 trunk 26704) [i686-linux]

A singleton class definition of nil pushes NilClass onto cref.  
It is reasonable because NilClass has nil as the only instance.  
However, nil.instance\_eval pushes nil onto cref, which means that method  
definitions are not permitted in that context.

defiant:ruby$ ruby-trunk -ve ‘class \<\<nil; def foo; puts “foo” end;  
end; nil.foo’  
ruby 1.9.2dev (2010-02-18 trunk 26704) [i686-linux]  
foo  
defiant:ruby$ ruby-trunk -ve ‘nil.instance\_eval {|i| def foo; puts  
“foo” end }; nil.foo’  
ruby 1.9.2dev (2010-02-18 trunk 26704) [i686-linux]  
-e:1:in `block in <main>': no class/module to add method (TypeError) from -e:1:in `instance\_eval’  
from -e:1:in `’

The behavior is the same in Ruby 1.8.7.  
Is it intended or a bug?

---

<div class="post-metadata">

**Author:** ![Shugo\_M](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/7ea924/32.png) [@Shugo\_M](https://www.ruby-forum.com/u/Shugo_M)\
**Post date:** [February 24, 2010, 2:09am UTC](https://www.ruby-forum.com/t/bug-2787-nil-instance-eval-pushes-nil-onto-cref/184032/2 "2010-02-24T02:09:56Z")

</div>

e$B%A%1%C%He(B #2787 e$B$,99?7$5$l$^$7$?!#e(B (by Shugo M.)

e$B%9%F!\<%?%9e(B Assignede$B$+$ie(BClosede$B$KJQ99e(B

## I’m sorry that I have sent this issue to the wrong ML. I’ll resubmit this issue later.

[http://redmine.ruby-lang.org/issues/show/2787](http://redmine.ruby-lang.org/issues/show/2787)
