Skip to content

Instantly share code, notes, and snippets.

@ivanacostarubio
Created May 16, 2012 18:01
Show Gist options
  • Select an option

  • Save ivanacostarubio/2712657 to your computer and use it in GitHub Desktop.

Select an option

Save ivanacostarubio/2712657 to your computer and use it in GitHub Desktop.
How to implement polymorphic behavior
class Animal
def initialize
end
def talk
raise NotImplementedError("Subclass must implement abstract method")
end
end
class Cat
def talk
return 'Meow!'
end
end
class Dog
def talk
return 'Woof! Woof!'
end
end
animals = [Cat.new,
Dog.new]
animals.each do |animal|
puts animal.talk()
end
class Animal
attr_accessor :type
def initialize
end
def talk
if type == "Cat"
return "Meow!"
elsif type "Dog"
return "Woof! Woof!"
else
return "WTF dude!"
end
end
end
@ivanacostarubio
Copy link
Copy Markdown
Author

caring about indentation misses the point of the gist. :trollface:

@orlando
Copy link
Copy Markdown

orlando commented May 16, 2012

hurts my eyes.... :trollface:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment