@JRubyMethod()
 public IRubyObject accept_nonblock(ThreadContext context, IRubyObject opts) {
   return doAcceptNonblock(
       this,
       context,
       ArgsUtil.extractKeywordArg(context, "exception", opts) != context.runtime.getFalse());
 }
Beispiel #2
0
  public static IRubyObject extractKeywordArg(
      ThreadContext context, String keyword, IRubyObject... args) {
    IRubyObject opts = ArgsUtil.getOptionsArg(context.runtime, args);

    if (!opts.isNil())
      return ((RubyHash) opts).op_aref(context, context.runtime.newSymbol(keyword));

    return context.nil;
  }
Beispiel #3
0
 public static IRubyObject[] extractKeywordArgs(
     ThreadContext context, IRubyObject[] args, String[] validKeys) {
   IRubyObject options = ArgsUtil.getOptionsArg(context.runtime, args);
   if (options instanceof RubyHash) {
     return extractKeywordArgs(context, (RubyHash) options, validKeys);
   } else {
     return null;
   }
 }
Beispiel #4
0
  @Override
  public IRubyObject interpret(
      Ruby runtime, ThreadContext context, IRubyObject self, Block aBlock) {
    IRubyObject value = getValueNode().interpret(runtime, context, self, aBlock);

    if (!(value instanceof RubyArray)) {
      value = ArgsUtil.convertToRubyArray19(runtime, value, pre != null);
    }

    return AssignmentVisitor.multiAssign(runtime, context, self, this, (RubyArray) value);
  }
Beispiel #5
0
 protected IRubyObject setupBlockArg(Ruby ruby, IRubyObject value, IRubyObject self) {
   switch (argumentType) {
     case ZERO_ARGS:
       return null;
     case MULTIPLE_ASSIGNMENT:
     case SINGLE_RESTARG:
       return ArgsUtil.convertToRubyArray(ruby, value, hasMultipleArgsHead);
     default:
       return defaultArgLogic(ruby, value);
   }
 }
 private IRubyObject prepareArrayArgsForCall(Ruby ruby, IRubyObject value) {
   int length = ArgsUtil.arrayLength(value);
   switch (length) {
     case 0:
       return ruby.getNil();
     case 1:
       return ((RubyArray) value).eltInternal(0);
     default:
       blockArgWarning(ruby, length);
   }
   return value;
 }
 protected IRubyObject[] assignArrayToBlockArgs(Ruby ruby, IRubyObject value) {
   switch (argumentType) {
     case ZERO_ARGS:
       return new IRubyObject[] {};
     case MULTIPLE_ASSIGNMENT:
     case SINGLE_RESTARG:
       return ArgsUtil.convertToJavaArray(value);
     default:
       {
         return new IRubyObject[] {prepareArrayArgsForCall(ruby, value)};
       }
   }
 }
Beispiel #8
0
  @Override
  public IRubyObject assign(
      Ruby runtime,
      ThreadContext context,
      IRubyObject self,
      IRubyObject value,
      Block block,
      boolean checkArity) {
    if (!(value instanceof RubyArray)) {
      value = ArgsUtil.convertToRubyArray19(runtime, value, pre != null);
    }

    return AssignmentVisitor.multiAssign(
        runtime, context, self, this, (RubyArray) value, checkArity);
  }
Beispiel #9
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};
  }
Beispiel #10
0
  public static IRubyObject[] setupArgs(
      Ruby runtime, ThreadContext context, Node node, IRubyObject self, Block aBlock) {
    if (node == null) return IRubyObject.NULL_ARRAY;

    if (node instanceof ArrayNode) {
      ArrayNode argsArrayNode = (ArrayNode) node;
      String savedFile = context.getFile();
      int savedLine = context.getLine();
      int size = argsArrayNode.size();
      IRubyObject[] argsArray = new IRubyObject[size];

      for (int i = 0; i < size; i++) {
        argsArray[i] = argsArrayNode.get(i).interpret(runtime, context, self, aBlock);
      }

      context.setFileAndLine(savedFile, savedLine);

      return argsArray;
    }

    return ArgsUtil.convertToJavaArray(node.interpret(runtime, context, self, aBlock));
  }