Last active
September 7, 2015 19:48
-
-
Save malisbad/2a6218aab6fe4644c02d 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
#Classical inheritance exercise | |
module Flight | |
attr_accessor :airspeed_velocity | |
def fly(airspeed_velocity) | |
return "I'm a #{self.class.to_s.downcase}, I'm flying!" | |
end | |
end | |
class Animal | |
attr_accessor :name, :blood_type | |
@@num_animals = 0 | |
def initialize(name, blood_type) | |
@@num_animals += 1 | |
@name = name | |
@blood_type = blood_type | |
end | |
def self.num_animals | |
@@num_animals | |
end | |
end | |
class Mammal < Animal | |
attr_accessor :num_legs | |
def initialize(name, num_legs) | |
super(name, "warm") | |
@num_legs = num_legs | |
end | |
end | |
class Amphibian < Animal | |
attr_accessor :terrestrial | |
def initialize(name, terrestrial) | |
super(name, "cold") | |
@terrestrial = terrestrial | |
end | |
end | |
class Primate < Mammal | |
attr_accessor :family | |
def initialize(name, family) | |
super(name, 2) | |
@family = family | |
end | |
end | |
class Frog < Amphibian | |
attr_accessor :poisonous | |
def initialize(name, poisonous) | |
super(name, "aquatic") | |
@poisonous = poisonous | |
end | |
end | |
class Bat < Mammal | |
include Flight | |
attr_accessor :species | |
def initialize(name, species, airspeed_velocity) | |
super(name, 2) | |
@species = species | |
puts fly(airspeed_velocity) | |
end | |
end | |
class Parrot < Animal | |
include Flight | |
attr_accessor :species | |
def initialize(name, species, airspeed_velocity) | |
super(name, "warm") | |
@species = species | |
puts fly(airspeed_velocity) | |
end | |
end | |
class Chimpanzee < Primate | |
attr_accessor :trained | |
def initialize(name, trained) | |
super(name, "hominidae") | |
@trained = trained | |
end | |
end | |
animal = Animal.new("Jimmy", "warm") | |
mammal = Mammal.new("Jimmy", 4) | |
primate = Primate.new("Jimmy", "hominidae") | |
chimpanzee = Chimpanzee.new("Jimmy", false) | |
amphibian = Amphibian.new("Jonny", "terrestrial") | |
frog = Frog.new("Jonny", false) | |
bat = Bat.new("Vlad", "Vampire Bat", 100) | |
parrot = Parrot.new("Cocky", "Cockatoo", 100) | |
puts animal.inspect | |
puts mammal.inspect | |
puts primate.inspect | |
puts chimpanzee.inspect | |
puts frog.inspect | |
puts amphibian.inspect | |
puts bat.inspect | |
puts parrot.inspect | |
puts Animal.num_animals |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment