Skip to content

Instantly share code, notes, and snippets.

@jimweirich
Created March 25, 2009 11:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jimweirich/85433 to your computer and use it in GitHub Desktop.
Save jimweirich/85433 to your computer and use it in GitHub Desktop.
class Animal
def set_from_animal
@@x = :animal
end
def get_from_animal
@@x
end
end
class Dog < Animal
def set_from_dog
@@x = :dog
end
def get_from_dog
@@x
end
end
require 'animals'
Dog.new.set_from_dog
Animal.new.set_from_animal
# Animal and Dog have distinct @@x variables
puts Animal.new.get_from_animal
puts Dog.new.get_from_dog
$ ruby shared.rb
dog
dog
$
$ ruby distinct.rb
animal
dog
$
require 'animals'
Animal.new.set_from_animal
Dog.new.set_from_dog
# Animal and Dog share an @@x variable
puts Animal.new.get_from_animal
puts Dog.new.get_from_dog
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment