ruby-changes:7671
From: matz <ko1@a...>
Date: Sat, 6 Sep 2008 23:58:29 +0900 (JST)
Subject: [ruby-changes:7671] Ruby:r19192 (trunk): * proc.c (Init_Proc): add '===' operator to use procs in when
matz 2008-09-06 23:58:11 +0900 (Sat, 06 Sep 2008) New Revision: 19192 http://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=rev&revision=19192 Log: * proc.c (Init_Proc): add '===' operator to use procs in when clause of the case statement. inspired by <http://www.aimred.com/news/developers/2008/08/14/unlocking_the_power_of_case_equality_proc/>. Modified files: trunk/ChangeLog trunk/proc.c Index: ChangeLog =================================================================== --- ChangeLog (revision 19191) +++ ChangeLog (revision 19192) @@ -140,6 +140,11 @@ a patch from Tadashi Saito <shiba at mail2.accsnet.ne.jp> in [ruby-dev:36102]. +Sat Sep 6 04:44:06 2008 Yukihiro Matsumoto <matz@r...> + + * proc.c (Init_Proc): add '===' operator to use procs in when + clause of the case statement. inspired by <http://www.aimred.com/news/developers/2008/08/14/unlocking_the_power_of_case_equality_proc/>. + Sat Sep 6 03:18:17 2008 Tanaka Akira <akr@f...> * transcode.c (allocate_converted_string): add arguments for a buffer Index: proc.c =================================================================== --- proc.c (revision 19191) +++ proc.c (revision 19192) @@ -466,12 +466,14 @@ * call-seq: * prc.call(params,...) => obj * prc[params,...] => obj + * prc.(params,...) => obj * * Invokes the block, setting the block's parameters to the values in * <i>params</i> using something close to method calling semantics. * Generates a warning if multiple values are passed to a proc that * expects just one (previously this silently converted the parameters - * to an array). + * to an array). Note that prc.() invokes prc.call() with the parameters + * given. It's a syntax sugar to hide "call". * * For procs created using <code>Kernel.proc</code>, generates an * error if the wrong number of parameters @@ -494,6 +496,14 @@ * from prog.rb:5 */ +/* + * call-seq: + * prc === obj => obj + * + * Invokes the block, with <i>obj</i> as the block's parameter. It is + * to allow a proc object to be a target of when clause in the case statement. + */ + static VALUE proc_call(int argc, VALUE *argv, VALUE procval) { @@ -1745,6 +1755,7 @@ rb_define_singleton_method(rb_cProc, "new", rb_proc_s_new, -1); rb_define_method(rb_cProc, "call", proc_call, -1); rb_define_method(rb_cProc, "[]", proc_call, -1); + rb_define_method(rb_cProc, "===", proc_call, -1); rb_define_method(rb_cProc, "yield", proc_call, -1); rb_define_method(rb_cProc, "to_proc", proc_to_proc, 0); rb_define_method(rb_cProc, "arity", proc_arity, 0); -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/