Anime Junkie ha escrito: > 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. That's funny. There is no pseudo-code AT ALL in Programming Ruby. What you see IS actual running Ruby code. It is a testament to how easy ruby is that it LOOKS like pseudo code. > def duration_in_mins=(new_duration) > @duration = (new_duration*60).to_i > end This is a function that takes a duration in minutes and converts (internally) into seconds. Its use is: # To specify a 3.2min song song.duration_in_mins = 3.2 puts song.duration # returns the song in seconds > > puts song.duration_in_mins # I assumed that this would print 2 but > produces the error "undefined method `duration_in_mins'" > And that's correct. There's no duration_in_mins function. You just defined a function duration_in_mins= (notice the EQUAL sign). That's a setter function, not a getter. > //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? No. Setters and Getters are two different functions. You need to specify both yourself, just like in any other language. Note, also, that in that example the actual setter never stores the result in minutes into the class. Thus, you can never get the result back as minutes, without an additional calculation. Now... if you add...(you can do this from irb): class Song def duration_in_mins @duration / 60.0 end end You'll get what you want. Hope that helps. --- Adding setters and getters can be rather tedious. Thus, for simple attributes that don't do any calculation, Ruby provides attr_accessor, attr_reader and attr_writer functions, that just add a getter/setter, just a getter or just a setter function automatically. Thus, your duration attribute can also be written as: class NewSong attr_accessor :duration attr_writer :another end song = NewSong.new song.duration = 20 puts song.duration song.another = 40 puts song.another ## fails, no reader, just a writer