Skip to content

Instantly share code, notes, and snippets.

@rtoal
Created July 17, 2013 07:32
Show Gist options
  • Save rtoal/6018426 to your computer and use it in GitHub Desktop.
Save rtoal/6018426 to your computer and use it in GitHub Desktop.
Demonstration of Inheritance and Polymorphism in Ruby
class Animal
def initialize(name)
@name = name
end
def speak()
"#{@name} says #{sound()}"
end
end
class Cow < Animal
def sound()
"moo"
end
end
class Horse < Animal
def sound()
"neigh"
end
end
class Sheep < Animal
def sound()
"baaaaa"
end
end
if __FILE__ == $0
s = Horse.new "CJ"
puts(s.speak())
c = Cow.new("Bessie")
puts(c.speak())
puts(Sheep.new("Little Lamb").speak())
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment