Hello all,
I’m not much of a developer but will be supporting RoR in the near
future and thought I would familiarize myself with Ruby a bit. I’m
using the “Programming Ruby” book and find that a lot of the examples
don’t work. They use too much pseudo code in my opinion. Anyhow, could
anyone explain why the following doesn’t work? I’ve put comments in
where I thought necessary.
class Song
attr_reader :duration
def initialize(duration)
@duration = duration
end
def duration_in_mins=(new_duration)
@duration = (new_duration*60).to_i
end
end # End Song
song = Song.new(360)
puts song.duration # this prints 360
song.duration_in_mins = 2
puts song.duration # this prints out 120
puts song.duration_in_mins # I assumed that this would print 2 but
produces the error “undefined method `duration_in_mins’”
//I know that duration_in_mins is a virtual attribute but if I can use
it for assigning value shouldn’t I be able to print that?
Any help is much appreciated.
Regards,
Seth