Keep simple & live Strong

home

include 和 extend 的区别

16 Apr 2010

请看下面的代码:

module Gary
  def hello
    puts "hello"
  end
  def self.world
    puts "world"
  end
end
class A
  include Gary
end
class B
  extend Gary
end
A.new.hello # => "hello"
B.hello #     => "hello"
A.world # throw error: undefined method `world' for A:Class (NoMethodError)
B.world # throw error: undefined method `world' for B:Class (NoMethodError)

结论:




comments powered by Disqus
Fork me on GitHub