ruby - NameError: undefined local variable or method -


i have written simple ruby script , failing @ compiling. don't know problem. suggestions appreciated.

class song  def initialize (name, album, duration)         @name = name         @album = album         @duration = duration end  def duration=(new_duration)         @duration=new _duration end  def to_s     "song: #@name #@album -- #@duration \n" end  song = song.new("abc", "i don't lie...", 2.04)  print song.to_s  song.duration = 3.89  print song.to_s 

after compiling getting following error:

ruby song.rb song: abc don't lie... -- 2.04 1. abc 2. don't lie... 3. 2.040000 nameerror: undefined local variable or method `_duration' #<song:0x3b500efd> duration= @ song.rb:21 song @ song.rb:37 (root) @ song.rb:1 

you should remove method altogether , replace attr_writer. less error-prone , faster:

class song   attr_writer :duration    def initialize (name, album, duration)     @name = name     @album = album     @duration = duration   end    def to_s     "song: #@name #@album -- #@duration \n"   end end 

Comments

Popular posts from this blog

javascript - DIV "hiding" when changing dropdown value -

node.js - Node - Passport Auth - Authed Post Route hangs on form submission -

Does Firefox offer AppleScript support to get URL of windows? -