protected RubyValue doStep(RubyValue toArg, RubyValue stepArg, RubyBlock block) { if ((toArg instanceof RubyFixnum) && (stepArg instanceof RubyFixnum)) { int i = this.value_; int end = ((RubyFixnum) toArg).value_; int diff = ((RubyFixnum) stepArg).value_; if (diff > 0) { while (i <= end) { RubyValue v = block.invoke(this, ObjectFactory.createFixnum(i)); if (block.breakedOrReturned()) { return v; } i += diff; } } else { while (i >= end) { RubyValue v = block.invoke(this, ObjectFactory.createFixnum(i)); if (block.breakedOrReturned()) { return v; } i += diff; } } return this; } return super.doStep(toArg, stepArg, block); }
// @RubyLevelMethod(name="each_index") public RubyValue each_index(RubyBlock block) { for (int i = 0; i < size(); i++) { RubyValue v = block.invoke(this, new RubyFixnum(i)); if (block.breakedOrReturned()) { return v; } } return this; }
// @RubyLevelMethod(name="reverse_each") public RubyValue reverse_each(RubyBlock block) { ListIterator /*<RubyValue>*/ ite = array_.listIterator(array_.size()); while (ite.hasPrevious()) { RubyValue v = block.invoke(this, (RubyValue) ite.previous()); if (block.breakedOrReturned()) { return v; } } return this; }
// @RubyLevelMethod(name="each") public RubyValue each(RubyBlock block) { // for (RubyValue item : array_) { for (Iterator iter = array_.iterator(); iter.hasNext(); ) { RubyValue item = (RubyValue) iter.next(); RubyValue v = block.invoke(this, item); if (block.breakedOrReturned()) { return v; } } return this; }
public RubyValue times(RubyBlock block) { int value = this.value_; for (int i = 0; i < value; ++i) { RubyValue v = block.invoke(this, ObjectFactory.createFixnum(i)); if (block.breakedOrReturned()) { return v; } } return this; }
@RubyLevelMethod(name = "loop", module = true) public static RubyValue loop(RubyValue receiver, RubyArray args, RubyBlock block) { if (null == block) { throw new RubyException(RubyRuntime.LocalJumpErrorClass, "in `loop': no block given"); } for (; ; ) { RubyValue v = block.invoke(receiver, args); if (block.breakedOrReturned()) { return v; } } }