【发布时间】:2016-10-13 04:02:51
【问题描述】:
我希望可以在另一个方法中确定给定返回的原始类/方法/等。例如:
# Ruby
class Stringify
def fancy(words)
return ">> #{words}"
end
def boring(words)
return "- #{words}"
end
end
class Yarnify
def fancy(words)
return ">> #{words}"
end
def boring(words)
return "- #{words}"
end
end
def printit(*args)
puts args
end
printit(Yarnify.new.boring("Hello"))
printit(Stringify.new.fancy("Hey"))
printit(Stringify.new.boring("Hi"))
printit(Yarnify.new.fancy("Heyo"))
# Output:
"- Hello"
">> Hey"
"- Hi"
">> Heyo"
如:
## Desired trace
def printit(*args)
puts args
puts "Originated from #{args.what_called.method} within #{args.what_called.class}."
end
printit(Yarnify.new.fancy("This is a return!"))
## Output
">> This is a return!"
"Originated from fancy within Yarnify."
args 的内容似乎在传递到printit 方法之前已执行。但我有一个用例,我动态混合多个看起来相同的输入,因此需要记录args 的源类/方法。到目前为止,在 args 上搜索 rubydocs 和使用公共/私有方法并没有帮助。有谁知道可以吗?
【问题讨论】:
标签: ruby methods nested return trace