Exemple #1
0
  /**
   * This name may be a bit misleading, since this also attempts to coerce array behavior using
   * to_ary.
   *
   * @param runtime The JRuby runtime
   * @param value The value to convert
   * @param coerce Whether to coerce using to_ary or just wrap with an array
   */
  public static RubyArray convertToRubyArray(Ruby runtime, IRubyObject value, boolean coerce) {
    if (value == null) {
      return RubyArray.newEmptyArray(runtime);
    }

    if (coerce) return convertToRubyArrayWithCoerce(runtime, value);

    // don't attempt to coerce to array, just wrap and return
    return RubyArray.newArrayLight(runtime, value);
  }
Exemple #2
0
  public static RubyArray convertToRubyArrayWithCoerce(Ruby runtime, IRubyObject value) {
    if (value instanceof RubyArray) return ((RubyArray) value);

    IRubyObject newValue = TypeConverter.convertToType(value, runtime.getArray(), "to_ary", false);

    if (newValue.isNil()) {
      return RubyArray.newArrayLight(runtime, value);
    }

    // empirically it appears that to_ary coersions always return array or nil, so this
    // should always be an array by now.
    return (RubyArray) newValue;
  }
Exemple #3
0
  public static RubyArray convertToRubyArrayWithCoerce(Ruby runtime, IRubyObject value) {
    if (value instanceof RubyArray) return ((RubyArray) value);

    IRubyObject newValue = TypeConverter.convertToType(value, runtime.getArray(), "to_ary", false);

    if (newValue.isNil()) {
      return RubyArray.newArrayLight(runtime, value);
    }

    // must be array by now, or error
    if (!(newValue instanceof RubyArray)) {
      throw runtime.newTypeError(newValue.getMetaClass() + "#" + "to_ary" + " should return Array");
    }

    return (RubyArray) newValue;
  }