str = "focus"
p str.upcase
str.define_singleton_method(:upcase) do
"singleton:" + self.send(:upcase)
end
p str.singleton_methods
p str.upcase
# "FOCUS"
# [:upcase]
# q.rb:6:in `block in <main>': stack level too deep (SystemStackError)
# from q.rb:6:in `block in <main>'
# from q.rb:6:in `block in <main>'
# from q.rb:6:in `block in <main>'
# from q.rb:6:in `block in <main>'
# from q.rb:6:in `block in <main>'
# from q.rb:6:in `block in <main>'
# from q.rb:6:in `block in <main>'
# from q.rb:6:in `block in <main>'
# ... 4355 levels...
# from q.rb:6:in `block in <main>'
# from q.rb:6:in `block in <main>'
# from q.rb:6:in `block in <main>'
# from q.rb:10:in `<main>'
如上,如果想在 define_singleton_method 中使用对象原来的方法 (例子中的 upcase 方法), 要怎么做呢?
如果在祖先链上有许多同名方法,怎么才能调用具体某个类下的那个方法呢? (或者中某个类开始向前查找)
谢谢