Skip to content

Instantly share code, notes, and snippets.

@cheald
Created November 5, 2014 23:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cheald/90741003f20b8125aeae to your computer and use it in GitHub Desktop.
Save cheald/90741003f20b8125aeae to your computer and use it in GitHub Desktop.
@JRubyMethod(frame = true)
public IRubyObject min(ThreadContext context, Block block) {
RubyObject receiver;
if (block.isGiven()) {
receiver = this;
} else {
int c = RubyComparable.cmpint(context, invokedynamic(context, begin, MethodNames.OP_CMP, end), begin, end);
boolean emptyRange = c > 0 || (c == 0 && isExclusive);
if(emptyRange) return context.runtime.getNil();
receiver = RubyArray.newArray(context.runtime, new IRubyObject[] {begin, end});
}
return Helpers.invokeSuper(context, receiver, block);
}
@JRubyMethod(frame = true, required = 1)
public IRubyObject min(ThreadContext context, IRubyObject arg, Block block) {
if (block.isGiven()) {
return Helpers.invokeSuper(context, this, arg, block);
} else {
int c = RubyComparable.cmpint(context, invokedynamic(context, begin, MethodNames.OP_CMP, end), begin, end);
boolean emptyRange = c > 0 || (c == 0 && isExclusive);
if(emptyRange) {
return RubyArray.newArray(context.runtime, 0);
} else {
// Shortcut - just take the first N elements. No need to sort, as ranges are implicitly sorted.
return first(context, arg);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment