在我的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
答案 0 :(得分:3)
这里有几件事情发生了:
end
方法def type
Semi-Hollow