summaryrefslogtreecommitdiff
path: root/kernel.rb
diff options
context:
space:
mode:
authorTakashi Kokubun <takashikkbn@gmail.com>2020-07-03 09:52:35 -0700
committerGitHub <noreply@github.com>2020-07-03 09:52:35 -0700
commitf3a0d7a2035e9f5e0c70effd55732607e3def263 (patch)
tree697ff4e3c14edb8005ea2c361f026cec557fa1c6 /kernel.rb
parente8010c7401764f54173ffbe8c2bde38cd6d216fa (diff)
Rewrite Kernel#tap with Ruby (#3281)
* Rewrite Kernel#tap with Ruby This was good for VM too, but of course my intention is to unblock JIT's inlining of a block over yield (inlining invokeyield has not been committed though). * Fix test_settracefunc About the :tap deletions, the :tap events are actually traced (we already have a TracePoint test for builtin methods), but it's filtered out by tp.path == "xyzzy" (it became "<internal:kernel>"). We could trace tp.path == "<internal:kernel>" cases too, but the lineno is impacted by kernel.rb changes and I didn't want to make it fragile for kernel.rb lineno changes.
Notes
Notes: Merged-By: k0kubun <takashikkbn@gmail.com>
Diffstat (limited to 'kernel.rb')
-rw-r--r--kernel.rb22
1 files changed, 22 insertions, 0 deletions
diff --git a/kernel.rb b/kernel.rb
index d00ba3a809..3404053d84 100644
--- a/kernel.rb
+++ b/kernel.rb
@@ -48,6 +48,28 @@ module Kernel
Primitive.rb_obj_clone2(freeze)
end
+ #
+ # call-seq:
+ # obj.tap {|x| block } -> obj
+ #
+ # Yields self to the block, and then returns self.
+ # 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}" }
+ # .to_a .tap {|x| puts "array: #{x}" }
+ # .select {|x| x.even? } .tap {|x| puts "evens: #{x}" }
+ # .map {|x| x*x } .tap {|x| puts "squares: #{x}" }
+ #
+ #--
+ # \private
+ #++
+ #
+ def tap
+ yield(self)
+ self
+ end
+
module_function
#