Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
class Animal
@@noise = nil
def speak
@@noise
end
end
class Dog < Animal
@@noise = 'Woof'
end
class Cat < Animal
@@noise = 'Meeeeoowww'
end
# > load 'classvars.rb'
# => true
# > Cat.new.speak
# => "Meeeeoowww"
# > Dog.new.speak
# => "Meeeeoowww"
@Br3nda

This comment has been minimized.

Copy link

@Br3nda Br3nda commented Aug 26, 2015

heh.

>>> class Animal:
...   noise = None
... 
>>> class Dog(Animal):
...   noise = 'Woof'
... 
>>> class Cat(Animal):
...   noise = 'meow'
... 
>>> 
>>> 
>>> print Cat.noise
meow
>>> print Dog.noise
Woof
>>> 
>>> print Cat().noise
meow
>>> print Dog().noise
Woof

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment