我寫了一個非常簡單的Ruby腳本,它在編譯時失敗。不知道究竟是什麼問題。任何建議表示讚賞。NameError:未定義的局部變量或方法
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 for #@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
編譯我收到以下錯誤後:
ruby Song.rb
Song: ABC for I don't lie... -- 2.04
1. ABC
2. I don't lie...
3. 2.040000
NameError: undefined local variable or method `_duration' for #<Song:0x3b500efd>
duration= at Song.rb:21
Song at Song.rb:37
(root) at Song.rb:1
您只需查看指定的行。你有一個工作! – Shoe