static public Method

Module.new

Module.new                   mod
Module.new {|mod| block }    mod

Creates a new anonymous module. If a block is given, it is passed the module object, and the block is evaluated in the context of this module using module_eval.

Fred = Module.new do
  def meth1
    "hello"
  end
  def meth2
    "bye"
  end
end
a = "my string"
a.extend(Fred)   #=> "my string"
a.meth1          #=> "hello"
a.meth2          #=> "bye"

Source Code

/*
*  call-seq:
*    Module.new                  => mod
*    Module.new {|mod| block }   => mod
*  
*  Creates a new anonymous module. If a block is given, it is passed
*  the module object, and the block is evaluated in the context of this
*  module using <code>module_eval</code>.
*     
*     Fred = Module.new do
*       def meth1
*         "hello"
*       end
*       def meth2
*         "bye"
*       end
*     end
*     a = "my string"
*     a.extend(Fred)   #=> "my string"
*     a.meth1          #=> "hello"
*     a.meth2          #=> "bye"
*/

static VALUE
rb_mod_initialize(module)
   VALUE module;
{
   if (rb_block_given_p()) {
       rb_mod_module_eval(0, 0, module);
   }
   return Qnil;
}
Comments

Have your say
Please use Textile formatting (click here for a cheat sheet). Use <code/> and <pre/> for code samples.
Click here to login with OpenID to to post comments.