Instance Methods Class Error: in `new': wrong number of arguments (given 7, expected 0) (ArgumentError)

huangapple go评论86阅读模式
英文:

Instance Methods Class Error: in `new': wrong number of arguments (given 7, expected 0) (ArgumentError)

问题

  1. class Quarterback
  2. attr_accessor :id, :firstname, :secondname, :club, :position, :price, :totalpoints
  3. def initialize(id, firstname, secondname, club, position, price, totalpoints)
  4. @id = id
  5. @firstname = firstname
  6. @secondname = secondname
  7. @club = club
  8. @position = position
  9. @price = price
  10. @totalpoints = totalpoints
  11. end
  12. def info
  13. "#{firstname}#{club} 工作,价格是 #{price}。"
  14. end
  15. sparky = Quarterback.new('1', 'John', 'Edgar', 'Liverpool', 'Forward', '100', '38')
  16. puts sparky.info
  17. 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:

  1. class Quarterback
  2. attr_accessor :id, :firstname, :secondname, :club, :position, :price, :totalpoints,
  3. def initialize(id, firstname, secondname, club, position, price, totalpoints)
  4. @id = id
  5. @firstname = firstname
  6. @secondname = secondname
  7. @club = club
  8. @position = position
  9. @price = price
  10. @totalpoints = totalpoints
  11. end
  12. def info
  13. "#{firstname} works for #{club} and has a cost of #{price}."
  14. end
  15. sparky = Quarterback.new('1', 'John', 'Edgar', 'Liverpool', 'Forward', '100', '38' )
  16. puts sparky.info
  17. end

Thanks for the help!

答案1

得分: 1

那是因为在第二行末尾有一个,

  1. attr_accessor :id, :firstname, :secondname, :club, :position, :price, :totalpoints,

需要替换为:

  1. 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

huangapple
  • 本文由 发表于 2023年2月18日 01:23:13
  • 转载请务必保留本文链接:https://go.coder-hub.com/75487421.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定