From: Vincent Fourmond Date: 2006-12-24T10:50:29+09:00 Subject: Re: Beginner issues Anime Junkie wrote: > 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'" In the code, you define the method duration_in_mins=, which handles the assignment of @duration. However, you don't define a method duration_in_mins to return its value in minutes. Adding def duration_in_mins @duration/60.0 end should do it. Cheers, Vince -- Vincent Fourmond, PhD student http://vincent.fourmond.neuf.fr/