这是我的代码:
class Person
def initialize(first_name, last_name, age)
@first_name = first_name
@last_name = last_name
@age = age
end
def first_name
puts @first_name
end
def last_name
puts @last_name
end
def age
puts @age
end
end
class Musician < Person
def initialize(first_name, last_name, age, instrument)
@first_name = first_name
@last_name = last_name
@age = age
@instrument = instrument
end
def instrument
puts @instrument
end
end
然后当我尝试执行以下操作时:
m = Musician.new("George", "Harrison", 58, "guitar")
m.first_name + " " + m.last_name + ": " + m.age.to_s
我收到一个错误:
在
<main>': undefined method
+' 中表示 nil:NilClass (NoMethodError)
为什么我不能只连接对象方法的结果?