Skip to content

Instantly share code, notes, and snippets.

@Oceantidote
Created January 21, 2021 18:08
Show Gist options
  • Save Oceantidote/67c69fd4819909acbc9d2dc660a50fa7 to your computer and use it in GitHub Desktop.
Save Oceantidote/67c69fd4819909acbc9d2dc660a50fa7 to your computer and use it in GitHub Desktop.
class Animal
SPECIES = ["lion", "elephant"]
attr_reader :name
def initialize(name)
@name = name
end
def self.phyla
%w[Deuterostomia Ecdysozoa Lophotrochozoa Radiata]
end
def eat(food)
"#{name} eats #{food}"
end
def self.species
"hello"
end
end
puts Animal.species
puts Animal::SPECIES
require_relative 'lion'
require_relative 'meerkat'
require_relative 'warthog'
animals = []
animals << Lion.new("Simba")
animals << Meerkat.new("Timon")
animals << Warthog.new("Pumba")
puts "The animals of the jungle roar"
animals.each do |animal|
p animal.talk
end
# Another way of writing the above iteration
# animals.each(&:talk)
require_relative "animal"
class Lion < Animal
def talk
"#{name} roars"
end
def eat(food)
"#{super(food)}. Law of the jungle!"
end
end
require_relative 'animal'
class Meerkat < Animal
def talk
"#{name} tries to sell you car insurance"
end
end
require_relative '../animal'
require_relative '../lion'
require_relative '../meerkat'
describe Meerkat do
describe "#eat" do
it "return a sentence about what the animal is eating" do
animal = Meerkat.new("timon")
sentence = animal.eat("a grub")
expect(sentence).to eq("timon eats a grub")
end
end
end
describe Lion do
describe "#talk" do
it "returns a sentence stating the lion is roaring" do
sentence = Lion.new("Simba").talk
expect(sentence).to eq("Simba roars")
end
end
describe "#eat" do
it "return a sentence about what the animal is eating" do
animal = Lion.new("simba")
sentence = animal.eat("a gazelle")
expect(sentence).to eq("simba eats a gazelle. Law of the jungle!")
end
end
end
describe Animal do
describe "#initialize" do
it "creates an Animal instance" do
animal = Animal.new("babe")
expect(animal).to be_an(Animal)
end
end
describe "#name" do
it "should return the name of the animal" do
name = Animal.new("babe").name
expect(name).to eq("babe")
end
end
describe "::phyla" do
it "returns a list of all the different aniomal species" do
phyla = Animal.phyla
expect(phyla.length).to eq(4)
end
end
describe "#eat" do
it "return a sentence about what the animal is eating" do
animal = Animal.new("babe")
sentence = animal.eat("a carrot")
expect(sentence).to eq("babe eats a carrot")
end
end
end
require_relative 'animal'
class Meerkat < Animal
def talk
"#{name} tries to sell you car insurance"
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment