用户定义的类Ruby错误

时间:2012-11-03 22:04:57

标签: ruby class

在我的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

1 个答案:

答案 0 :(得分:3)

这里有几件事情发生了:

  • 在第26行,您的end方法
  • 没有def type
  • 在第32行,您在源文件中有一个句点;这不会解析,并会导致问题。
  • 在第49行,您在Semi-Hollow
  • 之后省略了结束语