Override method from parent : Overriding Methods « Class « Ruby






Override method from parent



class CD
  include Comparable
  @@plays = 0
  attr_reader :name, :artist, :duration
  attr_writer :duration
  def initialize(name, artist, duration)
    @name     = name
    @artist   = artist
    @duration = duration
    @plays    = 0
  end
  def to_s
    "CD: #@name--#@artist (#@duration)"
  end
  def name
    @name
  end
  def inspect
    self.to_s
  end
  def <=>(other)
    self.duration <=> other.duration
  end
end

class CD
 def to_s
    puts "new to_s"
  
 end
end


d = CD.new("A", "B", 6)
d.to_s

 








Related examples in the same category

1.Overriding Existing Methods
2.override your own methods
3.Overriding Methods Demo