public Method

Module.extend_object(p1)

extend_object(obj)     obj

Extends the specified object by adding this module’s constants and methods (which are added as singleton methods). This is the callback method used by Object#extend.

module Picky
  def Picky.extend_object(o)
    if String === o
      puts "Can't add Picky to a String"
    else
      puts "Picky added to #{o.class}"
      super
    end
  end
end
(s = Array.new).extend Picky  # Call Object.extend
(s = "quick brown fox").extend Picky

produces:

Picky added to Array
Can't add Picky to a String

Source Code

/*
*  call-seq:
*     extend_object(obj)    => obj
*  
*  Extends the specified object by adding this module's constants and
*  methods (which are added as singleton methods). This is the callback
*  method used by <code>Object#extend</code>.
*     
*     module Picky
*       def Picky.extend_object(o)
*         if String === o
*           puts "Can't add Picky to a String"
*         else
*           puts "Picky added to #{o.class}"
*           super
*         end
*       end
*     end
*     (s = Array.new).extend Picky  # Call Object.extend
*     (s = "quick brown fox").extend Picky
*     
*  <em>produces:</em>
*     
*     Picky added to Array
*     Can't add Picky to a String
*/

static VALUE
rb_mod_extend_object(mod, obj)
   VALUE mod, obj;
{
   rb_extend_object(obj, mod);
   return obj;
}
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.