Created
June 29, 2015 23:22
-
-
Save leimd/1375037508473822263e to your computer and use it in GitHub Desktop.
Classical inheritance
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
module Flight | |
attr_accessor:airspeec_velocity | |
def fly | |
puts ("#{self.class} is flying") | |
end | |
end | |
class Animal | |
attr_reader :num_legs,:food ,:habitat,:reproduction,:alive,:cold_blooded | |
def initialize (args) | |
@num_legs = args[:num_legs] | |
@food = args[:food] | |
@habitat = args[:habitat] | |
@reproduction = args[:reproduction] | |
post_initialize(args) | |
@color = args[:color] | |
end | |
def post_initialize(args) | |
nil #Hook | |
end | |
def cold_blooded? | |
@cold_blooded | |
end | |
end | |
class Mammal < Animal | |
attr_reader :tooth_replacement | |
def post_initialize(args) | |
@tooth_replacement = true | |
@alive = args[:alive] | |
super | |
end | |
end | |
class Amphibian < Animal | |
def post_initialize(args) | |
@cold_blooded = true | |
super | |
end | |
end | |
class Primate < Mammal# monkey | |
attr_reader :include_human | |
def post_initialize(args) | |
@num_legs = 4 | |
@food = 'banana' | |
@habitat = 'Forest' | |
@include_human = args[:include_human] | |
super | |
end | |
end | |
class Frog < Amphibian | |
def post_initialize(args) | |
super | |
end | |
end | |
class Bat < Mammal | |
include Flight | |
def post_initialize(args) | |
super | |
end | |
end | |
class Parrot < Animal # no parent class defind for him | |
def post_initialize(args) | |
super | |
end | |
end | |
class Chimpanzee < Primate # a specific kind of animal | |
def post_initialize(args) | |
super | |
end | |
end | |
bat = Bat.new({}) | |
bat.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
require_relative 'animals' | |
describe Animal do | |
before(:each) do | |
@ani = Animal.new({num_legs:4}) | |
end | |
it "has four legs" do | |
expect(@ani.num_legs) == 4 | |
end | |
end | |
describe Mammal do | |
before(:each) do | |
@mal = Mammal.new({num_legs:4,food:'corn'}) | |
end | |
it "has tooth_replacement" do | |
expect(@mal.tooth_replacement) == true | |
end | |
end | |
describe Amphibian do | |
before(:each) do | |
@amp = Amphibian.new({num_legs:4,food:'vegi'}) | |
end | |
it "is cold blooded" do | |
expect(@amp.cold_blooded?) == true | |
end | |
end | |
describe Primate do | |
before(:each) do | |
@pri = Primate.new({num_legs:4,food:'anything',include_human:true}) | |
end | |
it "included human" do | |
expect(@pri.include_human) == true | |
end | |
end | |
describe Frog do | |
before(:each) do | |
@frog = Frog.new({color:"green",habitat:'Canada'}) | |
end | |
it "is not an instance of Amphibian" do | |
expect(@frog).to_not be_an_instance_of(Amphibian) | |
end | |
it "Amphibian is a parent class" do | |
expect(@frog).to be_kind_of(Amphibian) | |
end | |
it "Frog is cold Blooded" do | |
expect(@frog.cold_blooded?).to be true | |
end | |
end | |
describe Bat do | |
before(:each) do | |
little_bat = Bat.new() | |
end | |
it "Bat should be able to fly" do | |
expect(Bat.fly) | |
end | |
end | |
describe Parrot do | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment