Skip to content

Instantly share code, notes, and snippets.

@meaganewaller
Last active December 26, 2015 19:09
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 meaganewaller/7199459 to your computer and use it in GitHub Desktop.
Save meaganewaller/7199459 to your computer and use it in GitHub Desktop.
OCP Principle Animal
class Animal
def breathe(animal)
if animal == "cat"
puts "inhale and exhale"
elsif animal == "dog"
puts "inhale and exhale"
elsif animal == "cow"
puts "inhale and exhale"
end
end
def speak(animal)
if animal == "cat"
puts "meow"
elsif animal == "dog"
puts "bark"
elsif animal == "cow"
puts "moo"
end
end
end
cat = Animal.new
cat.breathe("cat") #=> "inhale and exhale"
cat.speak("cat") #=> "meow"
dog = Animal.new
dog.breathe("dog") #=> "inhale and exhale"
dog.speak("dog") #=> "bark"
cow = Animal.new
cow.breathe("cow") #=> "inhale and exhale"
cow.speak("cow") #=> "moo"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment