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