Last active
February 4, 2022 00:01
-
-
Save caioertai/a27869189a53ea1faa1058cc0b2cb998 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
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 |
This file contains hidden or 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
# 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 | |
This file contains hidden or 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
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 |
This file contains hidden or 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
require_relative "animal" | |
class Meerkat < Animal | |
def initialize(name) | |
super(name) | |
@sound_verb = "barks" | |
end | |
end |
This file contains hidden or 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
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