用户定义类Ruby错误

用户定义类Ruby错误,ruby,class,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 G

在我的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
吉他 #/classes/person.rb中的源代码 #带有实例变量@name、@brand、@type@strings和 #方法获取字符串。 课堂吉他 #当用户调用guist.new时调用initialize方法。 def初始化(名称、品牌、类型、字符串) @name=名称 @品牌=品牌 @类型=_类型 @字符串=_字符串 结束 #定义getter。 定义名称 返回@name 结束 def品牌 返回@品牌 结束 def类型 返回@type def字符串 返回@strings 结束 . def字符串=(值) @字符串=值 结束 def至美国 返回“#{name};#{brand};#{type};#{strings}” 结束 结束 吉他=[]
吉他这里有几件事:

  • 在第26行,您的
    def type
    方法没有
    end
  • 在第32行,源文件中有一个句点;这将不会解析,并且会导致问题
  • 在第49行,您在
    Semi-Hollow

    • 这里发生了几件事:

      • 在第26行,您的
        def type
        方法没有
        end
      • 在第32行,源文件中有一个句点;这将不会解析,并且会导致问题
      • 在第49行,您在
        Semi-Hollow

      半空心
      “White Falcon”、“Gretsch”、“Semi Hollow,6)”缺少右引号
      希望您使用支持语法高亮显示的代码编辑器。如上所示,语法高亮显示在该行之后完全混乱,颜色是直接线索。半空心
      “White Falcon”缺少右引号Gretsch”,“Semi-Hollow,6)
      希望您使用支持语法高亮显示的代码编辑器。正如您在上面所看到的,语法高亮显示在这条线之后是混乱的,颜色是一个直接的线索。