Skip to content

Instantly share code, notes, and snippets.

@amcaplan
Created May 29, 2014 19:48
Show Gist options
  • Save amcaplan/894910bc4036581aa10a to your computer and use it in GitHub Desktop.
Save amcaplan/894910bc4036581aa10a to your computer and use it in GitHub Desktop.
Configuring Animal speech with a single variable
class Animal
@@config = {}
def self.config
@@config
end
def speak
if self.class.config[:verbose] == true
verbose_speech
else
brief_speech
end
end
def verbose_speech
''
end
def brief_speech
''
end
end
class Dog < Animal
def verbose_speech
"WOOF! WOOF WOOF WOOF!"
end
def brief_speech
"WOOF!"
end
end
Dog.new.speak # => "WOOF!"
Animal.config[:verbose] = true
Dog.new.speak # => "WOOF! WOOF WOOF WOOF!"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment