Skip to content

Instantly share code, notes, and snippets.

@edmore
Created September 28, 2011 20:31
Show Gist options
  • Save edmore/1249170 to your computer and use it in GitHub Desktop.
Save edmore/1249170 to your computer and use it in GitHub Desktop.
Duck Typing
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