Skip to content

Instantly share code, notes, and snippets.

@Ebonkuab
Created January 11, 2016 22:50
Show Gist options
  • Save Ebonkuab/9657381896fc67b909dc to your computer and use it in GitHub Desktop.
Save Ebonkuab/9657381896fc67b909dc to your computer and use it in GitHub Desktop.
using the inheritance properties of ruby to display different properties for sub-classes (LIGHTHOUSE WEB DEV BOOTCAMP)
class Animal
attr_reader :height , :habitat
def initialize(height, habitat)
@height = height
@habitat = habitat
end
def wakeup (wakeup_sound)
puts " i make this sound #{wakeup_sound} when I wakeup"
end
end
module Flight
attr_reader :airspeed_velocity
@airspeed_velocity = 10
def fly ()
puts " I am flying like a bird "
end
end
class Mammal < Animal
attr_reader :no_of_legs
def initialize (no_of_legs)
@no_of_legs = no_of_legs
end
end
class Amphibian < Animal
end
class Primate < Mammal
def initialize ()
@no_of_legs = 2
end
end
class Frog < Amphibian
end
class Bat < Animal
def wakeup ()
#puts " i make this sound HOWLING when I wakeup"
super ("HOWLING")
end
end
class Parrot < Animal
include Flight
end
class Chimpanzee < Primate
attr_reader :color
def initialize ()
@color = "red"
end
def show_colour
puts " I am a type of Chimpanzee showing of my color which is #{color}"
end
end
#Testing code
goat =Mammal.new(4)
puts goat.wakeup("bleat")
monkey = Primate.new
puts " I am monkey with #{monkey.no_of_legs} legs"
batman = Bat.new(1.90,"cave")
puts " I am a type of bat and #{batman.height} tall and live in a #{batman.habitat}"
good_chimpanze = Chimpanzee.new
puts good_chimpanze.show_colour
batman1 = Bat.new(2,"cave1")
puts batman1.wakeup
speakingParrot = Parrot.new(0.05,"cage")
speakingParrot.fly
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment