Re: Why is top-level an object rather than just Object?
- From: John Mair <jrmair@xxxxxxxxx>
- Date: Sat, 23 Oct 2010 00:06:20 -0500
Thomas Sawyer wrote in post #956401:
On Oct 22, 3:36pm, Intransition <transf...@xxxxxxxxx> wrote:
It should not be. In fact, I think it is a mistake for main to
module Main
for a minute your objections, but concentrating instead on replicatingend
x = Y.new(10)
y = Y.new(20)
puts x #=> "10"
puts y #=> "20"
@a = 30
puts x #=> "30"
puts y #=> "30"
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"
@Thomas:
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
def self.to_a
@a || = 10
end
end
class Y
def self.to_a
@a ||= 20
end
end
X #=> 10
Y #=> 20
class Object
@a = 30
end
X #=> 10
Y #=> 20
@a #=> 30
Having the instance variables on the Object class would NOT cause any of
the conflicts you guys are talking about :)
@Thomas, @Robert,
Have you guys checked out 'wrapped loads' ? In my edit to my previous
answer I wrote this:
"Have you checked out the 'load' function in Ruby? it has a feature
called 'wrapped loads' that loads the file into an anonymous module.
This means the loaded file cannot affect the global namespace and any
constants including classes and modules are trapped within the
anonymous module. Just call load with a second argument of anything
other than nil."
John
--
Posted via http://www.ruby-forum.com/.
.
- Follow-Ups:
- Re: Why is top-level an object rather than just Object?
- From: Intransition
- Re: Why is top-level an object rather than just Object?
- References:
- Why is top-level an object rather than just Object?
- From: John Mair
- Re: Why is top-level an object rather than just Object?
- From: Intransition
- Re: Why is top-level an object rather than just Object?
- From: John Mair
- Re: Why is top-level an object rather than just Object?
- From: Intransition
- Why is top-level an object rather than just Object?
- Prev by Date: Re: Compiling Ruby on OSX, OpenSSL error and possible fix
- Next by Date: Re: scope when reopening method
- Previous by thread: Re: Why is top-level an object rather than just Object?
- Next by thread: Re: Why is top-level an object rather than just Object?
- Index(es):
Relevant Pages
|