include and extend

Correct Understand:
"extend" adds methods from a module into a class as class methods.
"include" adds methods from a module into a class as instance methods.


It is quite easy to demonstrate this actually.


module SomeModule
  def hi
    puts "hello"
  end
end

class ExtendSample
  extend SomeModule
end

ExtendSample.hi

class IncludeSample
  include SomeModule
end

IncludeSample.new.hi

你可能感兴趣的:(include)