public Method

Object.__send__(...)

obj.send(symbol [, args...])         obj
obj.__send__(symbol [, args...])     obj

Invokes the method identified by symbol, passing it any arguments specified. You can use __send__ if the name send clashes with an existing method in obj.

class Klass
  def hello(*args)
    "Hello " + args.join(' ')
  end
end
k = Klass.new
k.send :hello, "gentle", "readers"   #=> "Hello gentle readers"

Source Code

/*
*  call-seq:
*     obj.send(symbol [, args...])        => obj
*     obj.__send__(symbol [, args...])    => obj
*  
*  Invokes the method identified by _symbol_, passing it any
*  arguments specified. You can use <code>\_\_send__</code> if the name
*  +send+ clashes with an existing method in _obj_.
*     
*     class Klass
*       def hello(*args)
*         "Hello " + args.join(' ')
*       end
*     end
*     k = Klass.new
*     k.send :hello, "gentle", "readers"   #=> "Hello gentle readers"
*/

static VALUE
rb_f_send(argc, argv, recv)
   int argc;
   VALUE *argv;
   VALUE recv;
{
   VALUE vid;

   if (argc == 0) rb_raise(rb_eArgError, "no method name given");

   vid = *argv++; argc--;
   PUSH_ITER(rb_block_given_p()?ITER_PRE:ITER_NOT);
   vid = rb_call(CLASS_OF(recv), recv, rb_to_id(vid), argc, argv, 1, Qundef);
   POP_ITER();

   return vid;
}
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.