}
@JRubyMethod
public IRubyObject to_proc(ThreadContext context) {
StaticScope scope = context.runtime.getStaticScopeFactory().getDummyScope();
final CallSite site = new FunctionalCachingCallSite(symbol);
BlockBody body = new ContextAwareBlockBody(scope, Arity.OPTIONAL, BlockBody.SINGLE_RESTARG) {
private IRubyObject yieldInner(ThreadContext context, RubyArray array, Block block) {
if (array.isEmpty()) {
throw context.runtime.newArgumentError("no receiver given");
}
IRubyObject self = array.shift(context);
return site.call(context, self, self, array.toJavaArray(), block);
}
@Override
public IRubyObject yield(ThreadContext context, IRubyObject[] args, IRubyObject self,
Binding binding, Type type, Block block) {
RubyProc.prepareArgs(context, type, block.arity(), args);
return yieldInner(context, context.runtime.newArrayNoCopyLight(args), block);
}
@Override
public IRubyObject yield(ThreadContext context, IRubyObject value,
Binding binding, Block.Type type, Block block) {
return yieldInner(context, ArgsUtil.convertToRubyArray(context.runtime, value, false), block);
}
@Override
protected IRubyObject doYield(ThreadContext context, IRubyObject value, Binding binding, Type type) {
return yieldInner(context, ArgsUtil.convertToRubyArray(context.runtime, value, false), Block.NULL_BLOCK);
}
@Override
protected IRubyObject doYield(ThreadContext context, IRubyObject[] args, IRubyObject self, Binding binding, Type type) {
return yieldInner(context, context.runtime.newArrayNoCopyLight(args), Block.NULL_BLOCK);
}
@Override
public IRubyObject yieldSpecific(ThreadContext context, IRubyObject arg0, Binding binding, Block.Type type) {
return site.call(context, arg0, arg0);
}
@Override
public IRubyObject yieldSpecific(ThreadContext context, IRubyObject arg0, IRubyObject arg1, Binding binding, Block.Type type) {
return site.call(context, arg0, arg0, arg1);
}
@Override
public IRubyObject yieldSpecific(ThreadContext context, IRubyObject arg0, IRubyObject arg1, IRubyObject arg2, Binding binding, Block.Type type) {
return site.call(context, arg0, arg0, arg1, arg2);
}
@Override
public String getFile() {
return symbol;