[前][次][番号順一覧][スレッド一覧]

ruby-changes:4485

From: ko1@a...
Date: Fri, 11 Apr 2008 19:19:11 +0900 (JST)
Subject: [ruby-changes:4485] kazu - Ruby:r15977 (ruby_1_8): * object.c (rb_obj_tap): new method Object#tap; backported from 1.9. bug#19008

kazu	2008-04-11 19:18:55 +0900 (Fri, 11 Apr 2008)

  New Revision: 15977

  Modified files:
    branches/ruby_1_8/ChangeLog
    branches/ruby_1_8/object.c

  Log:
    * object.c (rb_obj_tap): new method Object#tap; backported from 1.9. bug#19008


  http://svn.ruby-lang.org/cgi-bin/viewvc.cgi/branches/ruby_1_8/ChangeLog?r1=15977&r2=15976&diff_format=u
  http://svn.ruby-lang.org/cgi-bin/viewvc.cgi/branches/ruby_1_8/object.c?r1=15977&r2=15976&diff_format=u

Index: ruby_1_8/ChangeLog
===================================================================
--- ruby_1_8/ChangeLog	(revision 15976)
+++ ruby_1_8/ChangeLog	(revision 15977)
@@ -1,3 +1,8 @@
+Fri Apr 11 19:14:30 2008  Kazuhiro NISHIYAMA  <zn@m...>
+
+	* object.c (rb_obj_tap): new method Object#tap; backported from
+	  1.9. bug#19008
+
 Fri Apr 11 18:58:09 2008  Kazuhiro NISHIYAMA  <zn@m...>
 
 	* process.c: new method Process.exec; backported from 1.9. bug#19006
Index: ruby_1_8/object.c
===================================================================
--- ruby_1_8/object.c	(revision 15976)
+++ ruby_1_8/object.c	(revision 15977)
@@ -495,6 +495,29 @@
 
 
 /*
+ *  call-seq:
+ *     obj.tap{|x|...}    => obj
+ *  
+ *  Yields <code>x</code> to the block, and then returns <code>x</code>.
+ *  The primary purpose of this method is to "tap into" a method chain,
+ *  in order to perform operations on intermediate results within the chain.
+ *
+ *	(1..10)                .tap {|x| puts "original: #{x.inspect}"}
+ *	  .to_a                .tap {|x| puts "array: #{x.inspect}"}
+ *	  .select {|x| x%2==0} .tap {|x| puts "evens: #{x.inspect}"}
+ *	  .map { |x| x*x }     .tap {|x| puts "squares: #{x.inspect}"}
+ *
+ */
+
+VALUE
+rb_obj_tap(VALUE obj)
+{
+    rb_yield(obj);
+    return obj;
+}
+
+
+/*
  * Document-method: inherited
  *
  * call-seq:
@@ -2684,6 +2707,7 @@
     rb_define_method(rb_mKernel, "instance_of?", rb_obj_is_instance_of, 1);
     rb_define_method(rb_mKernel, "kind_of?", rb_obj_is_kind_of, 1);
     rb_define_method(rb_mKernel, "is_a?", rb_obj_is_kind_of, 1);
+    rb_define_method(rb_mKernel, "tap", rb_obj_tap, 0);
 
     rb_define_private_method(rb_mKernel, "singleton_method_added", rb_obj_dummy, 1);
     rb_define_private_method(rb_mKernel, "singleton_method_removed", rb_obj_dummy, 1);

--
ML: ruby-changes@q...
Info: http://www.atdot.net/~ko1/quickml/

[前][次][番号順一覧][スレッド一覧]