class C1 def pr puts 'C1' end end class C2 < C1 def pr puts 'C2' super puts self.method(:pr).source_location end end c = C2.new c.pr
在上面的程序中,可以获得由super(在我们的情况下为C1 :: pr)执行的代码的位置,以及使用source_location方法获取C2 :: pr代码的位置?
解决方法
从ruby 2.2你可以使用super_method这样:
Class A def pr puts "pr" end end Class B < A def pr puts "Super method: #{method(:pr).super_method}" end end
当super_method返回一个方法时,可以链接它们来查找祖先:
def ancestor(m) m = method(m) if m.is_a? Symbol super_m = m.super_method if super_m.nil? return m else return ancestor super_m end end
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。