Ruby中方法继承、方法查找路径、constant查找路径
@(Ruby)
继承类方法和实例方法
class A
def method1
end
def self.method2
end
end
class B < A
def method3
end
def self.method4
end
end
b = B.new
b.method1 #nil
b.method3 #nil
B.method2 #nil
可以看到,B不仅继承了A的实例方法,还继承了A的 singleton方法。
方法调用路径
module Include
def call(level)
puts "#{level} include"
super(level + 1) rescue nil
end
end
module Prepend
def call(level)
puts "#{level} prepend"
super(level + 1) rescue nil
end
end
module Extend