Created
June 4, 2013 20:16
-
-
Save headius/5709175 to your computer and use it in GitHub Desktop.
Cleaned up Hash#fetch impl in JRuby
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@JRubyMethod(compat = RUBY1_9) | |
public IRubyObject fetch(ThreadContext context, IRubyObject key, Block block) { | |
Ruby runtime = context.runtime; | |
IRubyObject value = internalGet(key); | |
if (value == null) { | |
if (block.isGiven()) return block.yield(context, key); | |
throw runtime.newKeyError("key not found: " + key); | |
} | |
return value; | |
} | |
@JRubyMethod(compat = RUBY1_9) | |
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 = internalGet(key); | |
if (value == null) { | |
if (blockGiven) return block.yield(context, key); | |
return _default; | |
} | |
return value; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Yes, this reads great.