11

On ruby, what is the reason for include is private, while Object#extend is public?

Andrew Grimm
  • 78,473
  • 57
  • 200
  • 338
Vlad Zloteanu
  • 8,464
  • 3
  • 41
  • 58

3 Answers3

10

Object#extend has to be public, otherwise you wouldn't be able to use it. After all, its purpose is to mix in a module into an object, so you'd generally call it like obj.extend(Foo), which isn't possible with private methods.

Module#include is usually only used inside a module body like so:

class Bar
  include Foo
end

I.e. it is usually called without a receiver, so it doesn't have to be public. Of course, it doesn't have to be private either.

My guess is the reason why it is private is that it is more invasive, because it changes the behavior of every instance of Bar, whereas Object#extend only changes a single object. Therefore, Module#include is in some sense "more dangerous" and thus is made private.

I don't know whether that is the actual reason, but it is consistent with other similar methods like Module#define_method.

Jörg W Mittag
  • 363,080
  • 75
  • 446
  • 653
1

To be able to run Foo.include(Bar) at any point would most likely be a source of very nasty bugs.

Theo
  • 131,503
  • 21
  • 160
  • 205
1

To supplement Jörg W Mittag's answer, Object#extend can also be used to include module's instance methods to be used in the class level (which will also be available to all instances of that class):

module Foo
  def bar (baz)
  end
end

class Qux
  extend Foo

  bar 'asdf'
end
janechii
  • 1,055
  • 1
  • 11
  • 26