Created
September 28, 2011 20:31
-
-
Save edmore/1249170 to your computer and use it in GitHub Desktop.
Duck Typing
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
def make_a_sound(obj) | |
obj.sound! | |
end | |
def catch_make_a_sound(obj) | |
return obj.sound! if obj.respond_to? sound! | |
"#{obj.name} does not make a sound!!" | |
end | |
class Mouse | |
attr_accessor :name | |
end | |
class Dog | |
attr_accessor :name | |
def sound! | |
"woof!!" | |
end | |
end | |
class Cat | |
attr_accessor :name | |
def sound! | |
"meow!!" | |
end | |
end | |
class Duck | |
attr_accessor :name | |
def sound! | |
"quack!!" | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment