From: Elton Li Date: 2001-05-28T16:40:04+09:00 Subject: [ruby-talk:15833] newbie question --------------C555268C072C26894FC261C3 Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit Why does the following prints 4.0 but prints 60.0 when the statement in durationInMinutes= is changed to @duration = (value*60).to_i or self.duration = (value*60).to_i? I thought it should print 60.0 in all three cases. ---------------------------------- class Song attr_reader :name, :artist, :duration attr_writer :duration, :artist def initialize(name1, artist1, duration1) @name = name1 @artist = artist1 @duration = duration1 end def to_s "Song: #{@name}--#{@artist} (#{@duration})" end def durationInMinutes duration/60.0 end def durationInMinutes=(value) duration = (value*60).to_i end end aSong = Song.new("New Song", "New ArtistShadows", 240) aSong.durationInMinutes = 60.0 print aSong.durationInMinutes.to_s --------------C555268C072C26894FC261C3 Content-Type: text/html; charset=us-ascii Content-Transfer-Encoding: 7bit Why does the following prints 4.0 but prints 60.0 when the statement in durationInMinutes= is changed to

@duration = (value*60).to_i
or
self.duration = (value*60).to_i?

I thought it should print 60.0 in all three cases.
 
 

----------------------------------

class Song
  attr_reader :name, :artist, :duration
  attr_writer :duration, :artist
 
  def initialize(name1, artist1, duration1)
    @name   = name1
    @artist = artist1
    @duration = duration1
  end

  def to_s
    "Song: #{@name}--#{@artist} (#{@duration})"
  end

  def durationInMinutes
    duration/60.0
  end

  def durationInMinutes=(value)
    duration = (value*60).to_i
  end

end

aSong = Song.new("New Song", "New ArtistShadows", 240)
aSong.durationInMinutes = 60.0
print aSong.durationInMinutes.to_s --------------C555268C072C26894FC261C3--