Skip to content

Commit

Permalink
Move Kernel#tap into Ruby
Browse files Browse the repository at this point in the history
The Java version did little more than yield to the given block,
and the new specs provided for Kernel#warn skipping internal files
expects that Kernel#tap will come from an internal file.

There's a workaround here for behavior that changed in CRuby,
whereby yielding to a lambda should trigger arity errors rather
than bypassing arity checks.

See https://bugs.ruby-lang.org/issues/12705

See jruby#6430 for the "internal" source issue that led to moving this
into Ruby.
  • Loading branch information
headius committed Oct 10, 2023
1 parent 641a091 commit d7ab88d
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 14 deletions.
21 changes: 11 additions & 10 deletions core/src/main/java/org/jruby/RubyKernel.java
Original file line number Diff line number Diff line change
Expand Up @@ -2028,16 +2028,6 @@ public static IRubyObject fork19(ThreadContext context, IRubyObject recv, Block
return fork(context, recv, block);
}

@JRubyMethod(module = true)
public static IRubyObject tap(ThreadContext context, IRubyObject recv, Block block) {
if (block.getProcObject() != null) {
block.getProcObject().call(context, recv);
} else {
block.yield(context, recv);
}
return recv;
}

@JRubyMethod(name = {"to_enum", "enum_for"}, rest = true, keywords = true)
public static IRubyObject obj_to_enum(final ThreadContext context, IRubyObject self, IRubyObject[] args, final Block block) {
// to_enum is a bit strange in that it will propagate the arguments it passes to each element it calls. We are determining
Expand Down Expand Up @@ -2644,4 +2634,15 @@ public static IRubyObject method(IRubyObject self, IRubyObject symbol) {
public static IRubyObject method19(IRubyObject self, IRubyObject symbol) {
return method(self, symbol);
}

// defined in Ruby now but left here for backward compat
@Deprecated
public static IRubyObject tap(ThreadContext context, IRubyObject recv, Block block) {
if (block.getProcObject() != null) {
block.getProcObject().call(context, recv);
} else {
block.yield(context, recv);
}
return recv;
}
}
14 changes: 10 additions & 4 deletions core/src/main/ruby/jruby/kernel/kernel.rb
Original file line number Diff line number Diff line change
@@ -1,16 +1,22 @@
module Kernel
module_function
def exec(*args)
module_function def exec(*args)
_exec_internal(*JRuby::ProcessUtil.exec_args(args))
end

# Replaces Java version for better caching
def initialize_dup(original)
module_function def initialize_dup(original)
initialize_copy(original)
end

# Replaces Java version for better caching
def initialize_clone(original, freeze: false)
module_function def initialize_clone(original, freeze: false)
initialize_copy(original)
end

def tap(&b)
# workaround for yield to lambda not triggering arity error
# see https://bugs.ruby-lang.org/issues/12705
b.lambda? ? b.call(self) : yield(self)
self
end
end

0 comments on commit d7ab88d

Please sign in to comment.