Mega Code Archive

 
Categories / Ruby / Class
 

Basing One Class on Another

class Animal   def initialize(color)     @color = color   end   def get_color     return @color   end end class Dog < Animal   def initialize(color, sound)     super(color)     @sound = sound   end   def get_sound     return @sound   end end dog = Dog.new("brown", "Bark") puts "The new dog is " + dog.get_color puts "The new dog says: " + dog.get_sound + "" + dog.get_sound