这两种情况下的 self 是否相同?
class Person
def who_am_i?
puts self # self?
end
end
ted = Person.new
def ted.singleton_who_am_i?
puts self # self?
end
ted.who_am_i?
ted.singleton_who_am_i?
最佳答案
是的,看起来是这样:
class Person
def who_am_i?
puts self.to_yaml
end
end
ted = Person.new
def ted.singleton_who_am_i?
puts self.to_yaml
end
ted.who_am_i?
--- !ruby/object:Person {}
ted.singleton_who_am_i?
--- !ruby/object:Person {}
关于ruby - 正常方法中的 "self"与单例方法中的 "self"?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10302238/