self.included – including class methods from a module in Ruby

Viewed 5809

I read this post: Ruby modules - included do end block – but still don't get when you would use the self.included do ... end block in a module.

The post says that code in the block will be ran when you include the module, but what's the point of that if a module's sole purpose is to be included? Wouldn't that code need to be run anyway? That block doesn't need to be there in order for that code to be run, right?

What would be the difference between the two below:

module M
  def self.included(base)
    base.extend ClassMethods
    base.class_eval do
      scope :disabled, -> { where(disabled: true) }
    end
  end

  module ClassMethods
    ...
  end
end

vs.

module M
  def self.some_class_method
     ...
  end

  scope :disabled, -> { where(disabled: true) }
end
2 Answers
Related