Skip to content

Instantly share code, notes, and snippets.

@qxjit
Created February 7, 2012 18:19
Show Gist options
  • Save qxjit/1761077 to your computer and use it in GitHub Desktop.
Save qxjit/1761077 to your computer and use it in GitHub Desktop.
Please tell me this is a bug
# 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
@mustmodify
Copy link

This isn't a bug... it's a natural extension of the 1.8 behavior.

@qxjit
Copy link
Author

qxjit commented Feb 7, 2012

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.

@mustmodify
Copy link

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