Skip to content

Instantly share code, notes, and snippets.

@cprice404
Created September 11, 2012 22:04
Show Gist options
  • Save cprice404/3702455 to your computer and use it in GitHub Desktop.
Save cprice404/3702455 to your computer and use it in GitHub Desktop.
ruby class vars and inheritance
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