Created
February 7, 2012 18:19
-
-
Save qxjit/1761077 to your computer and use it in GitHub Desktop.
Please tell me this is a bug
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# ruby -v | |
# ruby 1.9.2p180 (2011-02-18 revision 30909) [x86_64-darwin10.8.0] | |
# | |
# note that you shouldn't use class variables anyhow... | |
class Foo | |
@@in_foo = 2 | |
def self.in_foo; @@in_foo; end | |
end | |
class Object | |
@@in_foo = 3 | |
end | |
puts Foo.in_foo #=> 3 |
It was a joke, but I was referring to this... taking it to Object is just the "obvious next step." :)
class Foo
@@value = 2
def self.value
@@value
end
end
class Bar < Foo
@@value = 3
end
Foo.value #=> 3
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
In ruby 1.8.7 (2011-02-18 patchlevel 334) [i686-darwin10.8.0] this program outputs 2, though. Object and Foo end up with two different class variables (both named @@in_foo). This behavior may be surprising, but it prevents someone writing code inside of Object from accidentally changing my Foo's class variable value because of an unexpected name collision. I don't see the 1.9.2p180 behavior as any sort of "natural" extension of 1.8.7's behavior.