Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: jruby/jruby
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: 146bbf54c0b5
Choose a base ref
...
head repository: jruby/jruby
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: 2f3f533266b2
Choose a head ref
  • 2 commits
  • 1 file changed
  • 2 contributors

Commits on Mar 16, 2018

  1. Copy the full SHA
    599d30c View commit details
  2. Merge pull request #5089 from ChrisBr/feature/thread#fetch

    Implement fetch for Thread.current
    headius authored Mar 16, 2018
    Copy the full SHA
    2f3f533 View commit details
Showing with 35 additions and 0 deletions.
  1. +35 −0 core/src/main/java/org/jruby/RubyThread.java
35 changes: 35 additions & 0 deletions core/src/main/java/org/jruby/RubyThread.java
Original file line number Diff line number Diff line change
@@ -88,6 +88,7 @@
import org.jruby.util.io.OpenFile;
import org.jruby.util.log.Logger;
import org.jruby.util.log.LoggerFactory;
import org.jruby.common.IRubyWarnings.ID;

import static org.jruby.runtime.Visibility.*;
import static org.jruby.runtime.backtrace.BacktraceData.EMPTY_STACK_TRACE;
@@ -922,6 +923,40 @@ public boolean isAlive(){
return threadImpl.isAlive() && status.get() != Status.DEAD;
}

@JRubyMethod
public IRubyObject fetch(ThreadContext context, IRubyObject key, Block block) {
Ruby runtime = context.runtime;

IRubyObject value = op_aref(key);

if (value.isNil()) {
if (block.isGiven()) return block.yield(context, key);

throw runtime.newKeyError("key not found: " + key.inspect(), this, key);
}

return value;
}

@JRubyMethod
public IRubyObject fetch(ThreadContext context, IRubyObject key, IRubyObject _default, Block block) {
Ruby runtime = context.runtime;
boolean blockGiven = block.isGiven();

if (blockGiven) {
runtime.getWarnings().warn(ID.BLOCK_BEATS_DEFAULT_VALUE, "block supersedes default value argument");
}

IRubyObject value = op_aref(key);

if (value.isNil()) {
if (blockGiven) return block.yield(context, key);
return _default;
}

return value;
}

@JRubyMethod(name = "[]", required = 1)
public synchronized IRubyObject op_aref(IRubyObject key) {
IRubyObject value;