Skip to content

Instantly share code, notes, and snippets.

@caioertai
Last active February 4, 2022 00:01
Show Gist options
  • Save caioertai/a27869189a53ea1faa1058cc0b2cb998 to your computer and use it in GitHub Desktop.
Save caioertai/a27869189a53ea1faa1058cc0b2cb998 to your computer and use it in GitHub Desktop.
class Animal
attr_reader :name
def initialize(name)
@name = name
end
def talk
"#{name} #{@sound_verb}"
end
def self.phyla
%w[Ecdysozoa Lophotrochozoa Deuterostomia]
end
def eat(food)
"#{name} eats #{food}."
end
end
# In another Ruby file, create an
# array with Simba, Nala, Timon &
# Pumbaa, iterate over it and puts
# the sound each animal make
require_relative "lion"
require_relative "warthog"
require_relative "meerkat"
animals = [
Lion.new("Simba"),
Lion.new("Nala"),
Warthog.new("Pumbaa"),
Meerkat.new("Timon"),
]
animals.each do |animal|
animal.talk
end
require_relative "animal"
class Lion < Animal
def initialize(name)
super(name)
@sound_verb = "roars"
end
def eat(food)
"#{super(food)} Law of the Jungle!"
end
end
require_relative "animal"
class Meerkat < Animal
def initialize(name)
super(name)
@sound_verb = "barks"
end
end
require_relative "animal"
class Warthog < Animal
def initialize(name)
super(name)
@sound_verb = "grunts"
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment