From: Rick DeNatale Date: 2010-10-23T02:21:26+09:00 Subject: Re: Why is top-level an object rather than just Object? On Fri, Oct 22, 2010 at 11:07 AM, John Mair wrote: > But, for most cases main already behaves like Object: > > * calling 'include' in main actually includes in Object > * defining a method in main actually defines the method on Object > * defining a constant in main actually defines the constant on Object I think there's some confusion here between self and the current class, and also about the differences in the relationship between classes and instances between Ruby and many other "OO" languages. At the top level self refers to the top level object. The current class is a separate value in the state of the ruby interpreter/vm which determines which class or module statements like def and include operate on. At the top level the current class is Object, and methods get defined as private by default. This makes them LOOK like functions rather than methods. > > The only notable exception, where 'main' acts like itself (that is - as > an instance of Object) -- is instance variables. main has its own > instance variables independent of Object. > > But i do not see a good reason not to just define the ivars on Object. > > See this post: > http://banisterfiend.wordpress.com/2010/10/22/what-is-the-ruby-top-level/ And here is another difference between Ruby and languages you might be "coming from." Instance variables are NOT "defined on" a class, but are rather dynamically attached to instances as methods run. http://talklikeaduck.denhaven2.com/2008/02/08/whose-variable-is-it-anyway -- Rick DeNatale Help fund my talk at Ruby Conf 2010:http://pledgie.com/campaigns/13677 Blog: http://talklikeaduck.denhaven2.com/ Github: http://github.com/rubyredrick Twitter: @RickDeNatale WWR: http://www.workingwithrails.com/person/9021-rick-denatale LinkedIn: http://www.linkedin.com/in/rickdenatale