J
John Mair
Why is it that top-level isn't just the Object class itself? what's the
rationale behind the 'main' object?
rationale behind the 'main' object?
Why is it that top-level isn't just the Object class itself? what's the
rationale behind the 'main' object?
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
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/
true, but I do not think it is a good ideaBut, for most cases main already behaves like Object:
* calling 'include' in main actually includes in Object
false; but as a private method, again I do not think it is a good idea* defining a method in main actually defines the method on Object
Oh but I do, why should Object have instance variables of the* defining a constant in main actually defines the constant on Object I buy this one
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. finally a good idea
But i do not see a good reason not to just define the ivars 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.
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.
true, but I do not think it is a good idea
false; but as a private method, again I do not think it is a good idea
Oh but I do, why should Object have instance variables of the
toplevel, if there are some good metaprogramming reasons to define
ivars on Object ... disaster.
Why is it that top-level isn't just the Object class itself? what's the
rationale behind the 'main' object?
It should not be. In fact, I think it is a mistake for main to
delegate to Object at all. It becomes a headache when trying to write
a scripting DSL. Rather than run the script at toplevel --which is
very nice b/c of the way #require works, one has to run the script in
a evaluation context so as to prevent pollution of every object. It is
easy enough to extend the Object class by opening it up or opening the
Kernel module, so this delegation is little more than a "nicety" --but
not a very nice one really.
Ideally main would simply be:
module Main
extend self
end
Constant lookup would have to be adjusted to go through Object and
then on to Main.
It still has to differ at least slightly in that top-level methods
have to be private Object methods.
Also, top-level instance could cause also sorts of messy:
=A0 class X
=A0 =A0 def initialize(a)
=A0 =A0 =A0 @a =3D a
=A0 =A0 end
=A0 =A0 def to_s; @a.to_s; end
=A0 end
=A0 class Y
=A0 =A0 def initialize(a)
=A0 =A0 =A0 @a =3D a
=A0 =A0 end
=A0 =A0 def to_s; @a.to_s; end
=A0 end
=A0 x =3D Y.new(10)
=A0 y =3D Y.new(20)
=A0 puts x =A0#=3D> "10"
=A0 puts y =A0#=3D> "20"
=A0 @a =3D 30
=A0 puts x =A0#=3D> "30"
=A0 puts y =A0#=3D> "30"
I believe that Tom (Intransition) has explained quite well. It is alsoRobert Dober wrote in post #956378:
Err, how exactly is it a disaster? can you explain? I can see no harm
coming from it. If you can see harm - then explain exactly what harm you
see rather than just communicating a vague sense of foreboding....hehe
;
My bad, that's not quite right. The @a would be at the class level,
so:
class X
def self.to_s
@a ||= 10
end
end
class Y
def self.to_s
@a ||= 20
end
end
puts X #=> "10"
puts Y #=> "20"
@a = 30
puts X #=> "30"
puts Y #=> "30"
Dude, instance variables are not inherited in this way. Every object
gets its OWN instance variable table, including the Object class. There
is no confusion and no mix up, behold:
class X
=A0 def self.to_a
=A0 =A0 @a || =3D 10
=A0 end
end
class Y
=A0 def self.to_a
=A0 =A0 @a ||=3D 20
=A0 end
end
X #=3D> 10
Y #=3D> 20
class Object
=A0 @a =3D 30
end
X #=3D> 10
Y #=3D> 20
@a #=3D> 30
Having the instance variables on the Object class would NOT cause any of
the conflicts you guys are talking about
Want to reply to this thread or ask your own question?
You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.