takaoueda / my-deja.com writes:

>  Now I am at page 74.  The overloading of the method + at the middle is
> clear.  But I got lost with the overloading of [] at the bottom.  I'd
> appreciate it if you would show a simple program that visualizes
> aSong[0, 0.15].  It would be better without the extra methods
> setStartTime and play.

The idea is that you create a partial song by specifying a start and
end offset into some other song. I'll change the example slightly to
make the start time an explicit parameter to the constructor.

  class Song
    def initialize(startTime, duration, mp3data)
      @startTime = startTime
      @duration = duration
      @mp3data = mp3data
    end
    def to_s
      "play #@mp3data starting at #@startTime for #@duration seconds"
    end
    def [](fromTime, toTime)
      Song.new(fromTime, toTime-fromTime, @mp3data)
    end
  end

  s_whole = Song.new(0, 100, "starlight.mp3")

  s_partial = s_whole[30, 40]

  puts s_whole        #=> play starlight.mp3 starting at 0 for 100 seconds
  puts s_partial      #=> play starlight.mp3 starting at 30 for 10 seconds


Hope this helps


Dave