Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"base.send :include, InstanceMethods" ---> What does this do?

Tags:

ruby

I'm looking at a module X which contains two modules called "InstanceMethods" and "ClassMethods".

The last definition in module X is this:

  def self.included(base)
    base.send :include, InstanceMethods
    base.send :extend,  ClassMethods
  end

What does this do?

like image 595
franz Avatar asked Jun 10 '09 04:06

franz


1 Answers

included gets called whenever a module is included into another module or class. In this case it will try to invoke base's include method to get the module methods, variables and constants from InstanceMethods added into base and then will try to invoke base's extend method to get the instance methods from ClassMethods added to base.

It could also have been

def self.included( base )
  base.include( InstanceMethods )
  base.extend( ClassMethods )
end
like image 113
toholio Avatar answered Sep 24 '22 08:09

toholio