Exemple #1
0
  /** num_step_scan_args */
  private IRubyObject[] scanStepArgs(ThreadContext context, IRubyObject[] args) {
    IRubyObject to = context.runtime.getNil();
    IRubyObject step = context.runtime.newFixnum(1);

    if (args.length >= 1) to = args[0];
    if (args.length >= 2) step = args[1];

    // TODO: Fold kwargs into the @JRubyMethod decorator
    IRubyObject[] kwargs = ArgsUtil.extractKeywordArgs(context, args, validStepArgs);

    if (kwargs != null) {
      to = kwargs[0];
      step = kwargs[1];

      if (!to.isNil() && args.length > 1) {
        throw context.runtime.newArgumentError("to is given twice");
      }
      if (!step.isNil() && args.length > 2) {
        throw context.runtime.newArgumentError("step is given twice");
      }
    } else {
      if (RubyBasicObject.equalInternal(context, step, RubyFixnum.zero(context.runtime))) {
        throw context.runtime.newArgumentError("step can't be 0");
      }
      if (step.isNil()) {
        throw context.runtime.newTypeError("step must be numeric");
      }
    }

    if (step.isNil()) {
      step = RubyFixnum.one(context.runtime);
    }

    if (to.isNil()) {
      if (f_negative_p(context, step)) {
        to = RubyFloat.newFloat(context.runtime, Double.NEGATIVE_INFINITY);
      } else {
        to = RubyFloat.newFloat(context.runtime, Double.POSITIVE_INFINITY);
      }
    }

    return new IRubyObject[] {to, step};
  }