好吧,我可以这样做:
class Person
attr_accessor :name
def greeting
"Hello #{@name}"
end
end
p = Person.new
p.name = 'Dave'
p.greeting # "Hello Dave"
但是当我决定在类本身中分配属性时它不起作用:
class Person
attr_accessor :name
@name = "Dave"
def greeting
"Hello #{@name}"
end
end
p = Person.new
p.greeting # "Hello"