0

在我的 ruby​​ 类中,我必须创建一个包含许多变量的用户定义类。到目前为止,我已经创建了一个与吉他相关的课程,并且我自己查看了一段时间的代码,但无法弄清楚为什么我在第 52 行/当我尝试打印出示例来测试课程时出现错误

# Guitar
# Source code in /classes/person.rb

# Guitar class with instance variables @name, @brand, @type @strings and
# method take_strings.

class Guitar

  # initialize method is called when user invokes Guitar.new.
  def initialize(the_name, the_brand, the_type, the_strings)
    @name = the_name
    @brand = the_brand
    @type = the_type
    @strings = the_strings
  end

  # Define getters.
  def name
    return @name
  end

  def brand
    return @brand
  end

  def type
    return @type

  def strings
    return @strings
  end
.
  def strings=(value)
    @strings = value
  end

  def to_s
    return "#{name}; #{brand}; #{type}; #{strings}."
  end

end


guitars = [ ]

guitars << Guitar.new("Stratocaster", "Fender", "Solid Body", 6)
guitars << Guitar.new("Les Paul", "Gibson", "Solid Body", 6)
guitars << Guitar.new("White Falcon", "Gretsch", "Semi-Hollow, 6)

# Print all guitars
guitars.each do |g|
  print g, "\n"
end
4

1 回答 1

3

这里有几件事:

  • 在第 26 行,您enddef type方法没有
  • 在第 32 行,您在源文件中有一个句点;这不会解析,并且会导致问题。
  • 在第 49 行,您在后面省略了结束引号Semi-Hollow
于 2012-11-03T22:12:13.280 回答