}
@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) {
if (array.isEmpty()) {
throw context.runtime.newArgumentError("no receiver given");
}
IRubyObject self = array.shift(context);
return site.call(context, self, self, array.toJavaArray());
}
@Override
public IRubyObject yield(ThreadContext context, IRubyObject value, Binding binding, Type type) {
return yieldInner(context, ArgsUtil.convertToRubyArray(context.runtime, value, false));
}
@Override
public IRubyObject yield(ThreadContext context, IRubyObject value, IRubyObject self, RubyModule klass, boolean aValue, Binding binding, Type type) {
RubyArray array = aValue && value instanceof RubyArray ?
(RubyArray) value : ArgsUtil.convertToRubyArray(context.runtime, value, false);
return yieldInner(context, array);
}
@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 Block cloneBlock(Binding binding) {
return new Block(this, binding);