-
-
Save amcaplan/894910bc4036581aa10a to your computer and use it in GitHub Desktop.
Configuring Animal speech with a single variable
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
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