summaryrefslogtreecommitdiff
path: root/object.c
diff options
context:
space:
mode:
authorkazu <kazu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2008-04-11 10:18:55 +0000
committerkazu <kazu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2008-04-11 10:18:55 +0000
commitb4c621b641f912cbc27fab01a73fcb84743c9d99 (patch)
tree5a6225e4f182a89ab9da690af304de8ce6d9d8a0 /object.c
parent4d78d30daa3535de1019eadde2e1a84d3b66873f (diff)
* object.c (rb_obj_tap): new method Object#tap; backported from 1.9. bug#19008
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/branches/ruby_1_8@15977 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'object.c')
-rw-r--r--object.c24
1 files changed, 24 insertions, 0 deletions
diff --git a/object.c b/object.c
index 3e2fb58..cd21f4e 100644
--- a/object.c
+++ b/object.c
@@ -495,6 +495,29 @@ rb_obj_is_kind_of(obj, c)
/*
+ * 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 @@ Init_Object()
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);