Slide 5
Slide 5 text
@JRubyMethod(name = {"collect", "map"}, compat = CompatVersion.RUBY1_8)
public static IRubyObject collect(ThreadContext context, IRubyObject self, final Block block) {
final Ruby runtime = context.getRuntime();
final RubyArray result = runtime.newArray();
if (block.isGiven()) {
callEach(runtime, context, self, block.arity(), new BlockCallback() {
public IRubyObject call(ThreadContext ctx, IRubyObject[] largs, Block blk) {
IRubyObject larg = checkArgs(runtime, largs);
IRubyObject value = block.yield(ctx, larg);
synchronized (result) {
result.append(value);
}
return runtime.getNil();
}
});
} else {
callEach(runtime, context, self, Arity.ONE_ARGUMENT, new AppendBlockCallback(runtime, result));
}
return result;
}