英文:
Instance Methods Class Error: in `new': wrong number of arguments (given 7, expected 0) (ArgumentError)
问题
class Quarterback
attr_accessor :id, :firstname, :secondname, :club, :position, :price, :totalpoints
def initialize(id, firstname, secondname, club, position, price, totalpoints)
@id = id
@firstname = firstname
@secondname = secondname
@club = club
@position = position
@price = price
@totalpoints = totalpoints
end
def info
"#{firstname} 在 #{club} 工作,价格是 #{price}。"
end
sparky = Quarterback.new('1', 'John', 'Edgar', 'Liverpool', 'Forward', '100', '38')
puts sparky.info
end
英文:
Hi I am just getting the following error: "in `new': wrong number of arguments (given 7, expected 0) (ArgumentError)"
In app/services/quarterback.rb I have:
class Quarterback
attr_accessor :id, :firstname, :secondname, :club, :position, :price, :totalpoints,
def initialize(id, firstname, secondname, club, position, price, totalpoints)
@id = id
@firstname = firstname
@secondname = secondname
@club = club
@position = position
@price = price
@totalpoints = totalpoints
end
def info
"#{firstname} works for #{club} and has a cost of #{price}."
end
sparky = Quarterback.new('1', 'John', 'Edgar', 'Liverpool', 'Forward', '100', '38' )
puts sparky.info
end
Thanks for the help!
答案1
得分: 1
那是因为在第二行末尾有一个,
:
attr_accessor :id, :firstname, :secondname, :club, :position, :price, :totalpoints,
需要替换为:
attr_accessor :id, :firstname, :secondname, :club, :position, :price, :totalpoints
关于为什么触发了该错误的更详细解释,请阅读这个很棒的答案:https://stackoverflow.com/a/58176158/11330290
英文:
That's because you have a ,
at the end of line 2:
attr_accessor :id, :firstname, :secondname, :club, :position, :price, :totalpoints,
to be replaced with
attr_accessor :id, :firstname, :secondname, :club, :position, :price, :totalpoints
For a more detailed explanation as to why that error was triggered, I would suggest to have a read at this great answer: https://stackoverflow.com/a/58176158/11330290
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论