Hi,
I write the code below on top level :
module A
def bar
self
end
end
include A # => Object
bar # => main
The top-level environment is special.
In the top-level methods get defined into Object as private methods by
definition, and the top-level self is a singular object. It is an instance
of Object with a singleton method #to_s that returns "main".
That top-level #include is not Modulel#include, it is a special-cased
include called top_include() in MRI that includes into Object by hand (see
eval.c). Grepping the source code sheds some light:
fxn@yeager:~/tmp/ruby-1.9.3-p327 $ ag
'rb_define_singleton_method.*top_self'
eval.c
1150: rb_define_singleton_method(rb_vm_top_self(), "include",
top_include, -1);
vm.c
2250: rb_define_singleton_method(rb_vm_top_self(), "to_s", main_to_s,
0);
vm_method.c
1305: rb_define_singleton_method(rb_vm_top_self(), "public",
top_public, -1);
1306: rb_define_singleton_method(rb_vm_top_self(), "private",
top_private, -1);
I don't think this is officially documented that way anywhere, as a
particular case of Ruby having not much documentation in general. You
should definitely buy Flanagan & Matsumoto to get your ideas ordered, but
even this book is not exhaustive, and in particular doesn't mention include
as a singleton method of the top-level self.
···
On Sat, Oct 19, 2013 at 7:27 AM, Love U Ruby <lists@ruby-forum.com> wrote: