=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
Assignee changed from matz (Yukihiro Matsumoto) to ko1 (Koichi Sasada)
Target version set to 2.0.0
Behavior is intended, as per tests in bootstraptest/test_eval.rb and [ruby-core:16808], but I'm don't think it's the right way to go, since nil, true and false do have singleton classes.
Koichi, do you agree the following patch would be good?
diff --git a/vm_eval.c b/vm_eval.c
index aa32621..dfd4588 100644
--- a/vm_eval.c
+++ b/vm_eval.c
@@ -1346,7 +1346,7 @@ rb_obj_instance_eval(int argc, VALUE *argv, VALUE self)
{
VALUE klass;
if (SPECIAL_CONST_P(self)) {
if (FIXNUM_P(self) || SYMBOL_P(self)) {
klass = Qnil;
}
else {
@@ -1378,7 +1378,7 @@ rb_obj_instance_exec(int argc, VALUE *argv, VALUE self)
{
VALUE klass;
test-all also points out that @@class_level_variable would then refer to NilClass/FalseClass/TrueClass's class variables, contrary to other singleton classes. This would be consistent with class << nil vs class << my_string.
Assignee changed from ko1 (Koichi Sasada) to matz (Yukihiro Matsumoto)
I'm so sorry to miss your ticket for long time.
Always I get confusing about instance eval with special variables (nil, false, etc).
Matz (shugo-san is specialist?), could you determine the specification?
This issue was solved with changeset r36647.
Shugo, thank you for reporting this issue.
Your contribution to Ruby is greatly appreciated.
May Ruby be with you.