From: "drbrain (Eric Hodel)" Date: 2012-06-22T03:20:47+09:00 Subject: [ruby-core:45757] [ruby-trunk - Feature #6609] Toplevel as self extended module Issue #6609 has been updated by drbrain (Eric Hodel). trans (Thomas Sawyer) wrote: > > What is a "partial Object class proxy"? It does not seem to exist. The top-level object (rb_vm_top_self) is an instance of Object where to_s is overridden to return "main" (see Init_top_self in vm.c). > > This post has the misconception that the top-level object is somehow special. > > It is "special" in that is delegates certain calls to the Object class. e.g. > > def foo; end > Object.private_instance_methods #=> [:foo] There are no method calls on the top-level object in this example. Nothing is delegated. > Hence it is a "proxy" for Object class. But it is not a complete proxy, e.g. it does not delegate "define_method", so it is "partial". You can't call define_method on an Object instance because there is no such method on an Object instance. Again, define_method only exists on Module and its subclasses. > > Since you are adding more things it seems more complicated. There are no extra things in the current top-level object. It's so simple it can be literally described as "the top-level object". > > To the contrary, top-level Object is not a generic instance of Object. I cannot find this in the ruby source code. Can you point me to a function that modifies rb_vm_top_self() in this way? I can only find where it is a generic instance of Object (see my previous comment). > What I am proposing is both simpler and more useful. > > > Such a change would break this: > > Yes, but such cases are rare and easily fixed. And you explain why that is so... > > > While this is a thing you should probably not write it does exist in the wild. > ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ^^^^^^^^^ > > Strike out "probably", and you have one of the reasons for this proposal. That people have done bad things is not a compelling reason. ---------------------------------------- Feature #6609: Toplevel as self extended module https://bugs.ruby-lang.org/issues/6609#change-27339 Author: trans (Thomas Sawyer) Status: Open Priority: Normal Assignee: Category: core Target version: 2.0.0 As promised sometime back here is my proposal that Toplevel object become a self-extended module instead of the current partial Object class proxy. I have written about it in a blog post: http://trans.github.com/2012/06/17/kill-the-proxy-and-save-toplevel.html In summary the basic idea is to have a special toplevel namespace that is self-extended, e.g. module Toplevel extend self end in which all toplevel code is evaluated. Definitions at the toplevel would no longer inject into Object class. This frees up the toplevel to be used for general purpose DSL "batch" scripting. What I mean by that is that one can create a DSL, load it in to toplevel and then evaluate scripts based on it simply by load/require and without fret that the code loaded in will infect Object if it defines it's own methods. Conceptually the idea of self-extended module is much simpler than current proxy object --there is really nothing special to understand about it since it is just a module like any other module. With regard to backward compatibility, the only programs that would be effected are any that defined a toplevel method fully expecting it to add a method to Object. But those will be very rare since it is generally considered bad form to do that. (And of course the simple fix is to wrap the method in the proper `class Object private ... end` code. -- http://bugs.ruby-lang.org/