Project

General

Profile

ActionsLike0

Bug #2788

closed

nil.instance_eval pushes nil onto cref

Added by shugo (Shugo Maeda) about 15 years ago. Updated over 12 years ago.

Status:
Closed
Target version:
ruby -v:
ruby 1.9.2dev (2010-02-18 trunk 26704) [i686-linux]
Backport:
[ruby-core:28324]

Description

=begin
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?
=end

Added by nobu (Nobuyoshi Nakada) over 12 years ago

Revision 28827e61

method in instance_eval

  • class.c (rb_special_singleton_class_of): utility function.
  • vm_eval.c (eval_under): special deal for class variable scope with
    instance_eval.
  • vm_eval.c (rb_obj_instance_eval, rb_obj_instance_exec): allow method
    definition in instance_eval of special constants. [ruby-core:28324]
    [Bug #2788]

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@36647 b2dd03c8-39d4-4d8f-98ff-823fe69b080e

ActionsLike0

Also available in: Atom PDF