# encoding: utf-8
class Person
attr_reader :short_name
def initialize(short_name)
@short_name = short_name
end
def greeting_line
short_name = short_name.downcase
"Hello #{short_name}"
end
end
person = Person.new("MS. LEE")
puts person.short_name => "MS. LEE"
puts person.greeting_line => NoMethodError: undefined method `downcase' for nil:NilClass
异常发生在“short_name = short_name.downcase”,因为 (short_name = short_name) 使 short_name 变为 nil。
为什么右侧的“short_name”没有从实例方法“short_name”获取值?