Created
September 11, 2012 22:04
-
-
Save cprice404/3702455 to your computer and use it in GitHub Desktop.
ruby class vars and inheritance
This file contains 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
irb | |
1.8.7 :001 > class Foo | |
1.8.7 :002?> class << self | |
1.8.7 :003?> def setvar(val) | |
1.8.7 :004?> @myvar = val | |
1.8.7 :005?> end | |
1.8.7 :006?> end | |
1.8.7 :007?> end | |
=> nil | |
1.8.7 :008 > class Bar < Foo | |
1.8.7 :009?> end | |
=> nil | |
1.8.7 :010 > Foo.setvar("foo") | |
=> "foo" | |
1.8.7 :011 > Bar.setvar("bar") | |
=> "bar" | |
1.8.7 :013 > Foo.instance_variable_get(:@myvar) | |
=> "foo" | |
1.8.7 :014 > Bar.instance_variable_get(:@myvar) | |
=> "bar" | |
1.8.7 :015 > |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment