Skip to content

Instantly share code, notes, and snippets.

@joshuastr
Created May 2, 2016 22:37
Show Gist options
  • Save joshuastr/e721def1f9988ee89c886d1fc8ad280e to your computer and use it in GitHub Desktop.
Save joshuastr/e721def1f9988ee89c886d1fc8ad280e to your computer and use it in GitHub Desktop.
Animals Inheritance
#require './animals_module'
module CanFly
def fly
puts "I'm a #{self.class.name}, and I can fly"
end
end
class Animal
attr_accessor :name,:blood_type, :num_legs
# def blood_type
# @blood_type = blood _type
# end
# def num_legs
# @num_legs = num_legs
# end
def initialize(name, blood_type, num_legs)
@blood_type = blood_type
@num_legs = num_legs
@name = name
puts "#{name} is #{blood_type} blooded, and has #{num_legs} legs."
end
end
#---------------
#TYPES OF ANIMALS
#---------------
class Mammal < Animal
def initialize(name, num_legs)
super(name, 'warm', num_legs)
end
end
class Amphibian < Animal
def initialize(name, num_legs)
super(name, 'cold', num_legs)
end
end
class Primate < Mammal
def initialize(name)
super(name, 2)
end
end
class Bird < Animal
include CanFly
def initialize(name)
super(name,'warm', 2)
end
end
#---------------
#ANIMALS
#---------------
class Bat < Mammal
include CanFly
def initialize(name)
super(name, 2)
end
end
class Frog < Amphibian
def initialize(name)
super(name, 4)
end
end
class Parrot < Bird
def initialize(name)
super
end
end
class Chimpanzee < Primate
def initialize(name)
super
end
end
charles = Parrot.new('charles')
charles.fly
josh = Primate.new('josh')
josh.inspect
kermit = Frog.new('kermit')
kermit.inspect
jeff = Bat.new('jeff')
jeff.fly
adam = Chimpanzee.new('adam')
adam.inspect
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment