Last active
November 4, 2015 05:18
-
-
Save lkhedlund/b2eda68c6a322d5df1cf to your computer and use it in GitHub Desktop.
In this exercise, we will take a step back from testing and create a mini program with multiple classes that inherit from each other
This file contains 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
#init | |
module Flight | |
attr_accessor :airspeed_velocity | |
def fly | |
"I'm a #{self.class.name}, I'm flying!" | |
end | |
end | |
# ============================= | |
class Animal | |
def exist? | |
true | |
end | |
end | |
# ============================== | |
class Mammal < Animal | |
attr_reader :blood_temp, :num_legs, :num_arms | |
def initialize | |
@blood_temp = "warm" | |
@num_legs = 2 | |
@num_arms = 2 | |
end | |
end | |
class Primate < Mammal | |
attr_reader :hairy | |
def initialize | |
@hairy = "yes" | |
super | |
end | |
end | |
class Chimpanzee < Primate | |
def uses_tools? | |
true | |
end | |
end | |
class Bat < Mammal | |
include Flight | |
attr_accessor :wings | |
def initialize | |
@wings = "Leathery" | |
end | |
end | |
# ====================== | |
class Amphibian < Animal | |
attr_reader :blood_temp | |
def initialize | |
@blood_temp = "cold" | |
end | |
end | |
class Frog < Amphibian | |
attr_reader :hops | |
def initialize | |
@hops = true | |
super | |
end | |
end | |
# ====================== | |
class Bird < Animal | |
attr_reader :beak | |
def initialize | |
@beak = "yes" | |
end | |
end | |
class Parrot < Bird | |
include Flight | |
def colourful? | |
true | |
end | |
end | |
# koko = Chimpanzee.new | |
# puts koko.inspect | |
# puts "Exists? #{koko.exist?}" | |
# | |
# | |
# frogger = Frog.new | |
# puts frogger.inspect | |
# puts "Blood temp: #{frogger.blood_temp}" | |
# puts "Hops: #{frogger.hops}" | |
# | |
# polly = Parrot.new | |
# puts polly.inspect | |
# puts "Colourful: #{polly.colourful?}" | |
# puts polly.fly | |
# | |
# puts Bat.new.fly |
This file contains 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
#rspec | |
require "./classical_inheritance" | |
# describe Animal do | |
# it "can create Animals" do | |
# expect(Animal.new).to be_a Animal | |
# end | |
# end | |
describe Chimpanzee do | |
it "creates Chimpanzees" do | |
expect(Chimpanzee.new).to be_a Chimpanzee | |
end | |
describe "#exist?" do | |
it "returns true if Chimpanzee inherits from Animal" do | |
koko = Chimpanzee.new | |
exists = koko.exist? | |
expect(exists).to be true | |
end | |
end | |
end | |
describe Frog do | |
it "creates Frogs" do | |
frogger = Frog.new | |
expect(frogger).to be_a Frog | |
end | |
it "has a blood temp of cold" do | |
frogger = Frog.new | |
result = frogger.blood_temp | |
expect(result).to eq("cold") | |
end | |
end | |
describe Parrot do | |
it "creates Parrots" do | |
polly = Parrot.new | |
expect(polly).to be_a Parrot | |
end | |
it "flies if it inherits fly from Flight" do | |
polly = Parrot.new | |
result = polly.fly | |
expect(result).to eq("I'm a Parrot, I'm flying!") | |
end | |
end | |
describe Bat do | |
it "flies if it inherits fly from Flight" do | |
stella = Bat.new | |
result = stella.fly | |
expect(result).to eq("I'm a Bat, I'm flying!") | |
end | |
end | |
# puts Bat.new.fly |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment