ruby 中 include 与 extend 区别
那么include和extend究竟有什么区别的?
引用如下
include : mixes in specified module methods as instance methods in the target class
extend : mixes in specified module methods as class methods in the target class
记得有看过的,被用到的时候居然忘记了。杯具啊!
module A
def klass_method
puts 'klass method'
end
def ins_method
puts 'instance method'
end
end
class B
end
B.class_eval do
include A
end
B.new.ins_method # 'should puts instance method'
#B.klass_method # 'not define'
B.class_eval do
extend A
end
B.klass_method # 'klass method'
B.ins_method # also as class method