public static IRubyObject checkIntegerType(Ruby runtime, IRubyObject obj, String method) { if (method.equals("to_int")) return checkIntegerType(runtime.getCurrentContext(), obj); if (obj instanceof RubyFixnum) return obj; if (method.equals("to_i")) { ThreadContext context = runtime.getCurrentContext(); TypeConverterSites sites = sites(context); IRubyObject conv = convertToTypeWithCheck(context, obj, runtime.getInteger(), sites.to_i_checked); return conv instanceof RubyInteger ? conv : runtime.getNil(); } IRubyObject conv = TypeConverter.convertToType(obj, runtime.getInteger(), method, false); return conv instanceof RubyInteger ? conv : runtime.getNil(); }
@Override @Deprecated public synchronized Object dataGetStructChecked() { TypeConverter.checkData(this); return getInternalVariable("__wrap_struct__"); }
public static IRubyObject getOptionsArg(Ruby runtime, IRubyObject... args) { if (args.length >= 1) { return TypeConverter.checkHashType(runtime, args[args.length - 1]); } return runtime.getNil(); }
public static IRubyObject checkIntegerType(ThreadContext context, IRubyObject obj) { if (obj instanceof RubyFixnum) return obj; TypeConverterSites sites = sites(context); IRubyObject conv = convertToTypeWithCheck(context, obj, context.runtime.getInteger(), sites.to_int_checked); return conv instanceof RubyInteger ? conv : context.nil; }
public static RubyArray rb_Array(ThreadContext context, IRubyObject val) { IRubyObject tmp = checkArrayType(context, val); // to_ary if (tmp == context.nil) { TypeConverterSites sites = sites(context); tmp = convertToTypeWithCheck(context, val, context.runtime.getArray(), sites.to_a_checked); if (tmp == context.nil) { return context.runtime.newArray(val); } } return (RubyArray) tmp; }
private static IRubyObject checkMaxInt(ThreadContext context, IRubyObject vmax) { if (!(vmax instanceof RubyFloat)) { IRubyObject v = TypeConverter.checkIntegerType(context, vmax); if (v != context.nil) return v; } return null; }
/** * Converts this object to type 'targetType' using 'convertMethod' method (MRI: convert_type). * * @param obj the object to convert * @param target is the type we are trying to convert to * @param convertMethod is the method to be called to try and convert to target type * @param raise will throw an Error if conversion does not work * @return the converted value */ public static IRubyObject convertToType(IRubyObject obj, RubyClass target, String convertMethod, boolean raise) { return convertToType(target.getClassRuntime().getCurrentContext(), obj, target, convertMethod, raise); }
/** * Converts this object to type 'targetType' using 'convertMethod' method (MRI: convert_type 1.9). * * @param obj the object to convert * @param target is the type we are trying to convert to * @param sites is the CheckedSites call sites to be called to try and convert to targetType * @param raise will throw an Error if conversion does not work * @return the converted value */ public static IRubyObject convertToType(ThreadContext context, IRubyObject obj, RubyClass target, JavaSites.CheckedSites sites, boolean raise) { IRubyObject r = obj.checkCallMethod(context, sites); return r == null ? handleUncoercibleObject(context.runtime, obj, target, raise) : r; }
/** rb_check_array_type * * Returns the result of trying to convert this object to an Array * with "to_ary". */ public IRubyObject checkArrayType() { return TypeConverter.convertToTypeWithCheck(this, getRuntime().getArray(), "to_ary"); }
/** * Checks that this object is of type DATA and then returns it, otherwise raises failure (MRI: Check_Type(obj, T_DATA)) * * @param obj the object to check * @return the converted value */ public static final IRubyObject checkData(IRubyObject obj) { if(obj instanceof org.jruby.runtime.marshal.DataType) return obj; throw obj.getRuntime().newTypeError("wrong argument type " + typeAsString(obj) + " (expected Data)"); }
private static float obj2flt(Ruby runtime, IRubyObject o) { return (float) toFloat(runtime, o).getDoubleValue(); }
} else if ((v = checkMaxInt(context, vmax)) != null) { v = randInt(context, random, (RubyInteger) v, true); } else if ((v = TypeConverter.checkFloatType(context.runtime, vmax)) != nil) { double max = ((RubyFloat) v).getDoubleValue(); if (max > 0.0) { v = nil; } else if ((v = TypeConverter.checkFloatType(context.runtime, range.range)) != nil) { int scale = 1; double max = ((RubyFloat) v).getDoubleValue(); double r; if (Double.isInfinite(max)) { double min = floatValue(context, toFloat(context.runtime, range.begin)) / 2.0; max = floatValue(context, toFloat(context.runtime, range.end)) / 2.0; scale = 2; mid = max + min; IRubyObject f = TypeConverter.checkFloatType(context.runtime, range.begin); if (f != nil) return ((RubyFloat) v).op_plus(context, f);
private static RubyFloat needFloat(IRubyObject x) { if (x instanceof RubyFloat) { return (RubyFloat)x; } if (!x.getRuntime().getNumeric().isInstance(x)) { TypeConverter.handleUncoercibleObject(true, x, x.getRuntime().getFloat()); } return (RubyFloat) TypeConverter.convertToType19(x, x.getRuntime().getFloat(), "to_f", true); }
/** rb_ary_to_ary * */ public static RubyArray aryToAry(ThreadContext context, IRubyObject obj) { IRubyObject tmp = TypeConverter.checkArrayType(context, obj); return tmp != context.nil ? (RubyArray) tmp : newArray(context.runtime, obj); }
} else if ((v = checkMaxInt(context, vmax)) != null) { v = randInt(context, random, (RubyInteger) v, true); } else if (!(v = TypeConverter.checkFloatType(context.runtime, vmax)).isNil()) { double max = floatValue(v); if (max > 0.0) { v = context.nil; } else if (!(v = TypeConverter.checkFloatType(context.runtime, range.range)).isNil()) { int scale = 1; double max = ((RubyFloat) v).getDoubleValue(); return ((RubyBignum) v).op_plus(context, range.begin); } else if (v instanceof RubyFloat) { IRubyObject f = TypeConverter.checkFloatType(context.runtime, range.begin); if (!f.isNil()) { return ((RubyFloat) v).op_plus(context, f);
private static IRubyObject checkMaxInt(ThreadContext context, IRubyObject vmax) { if (!(vmax instanceof RubyFloat)) { IRubyObject v = TypeConverter.checkIntegerType(context, vmax); if (v != context.nil) return v; } return null; }
public static IRubyObject aryToAry(ThreadContext context, IRubyObject value) { if (value instanceof RubyArray) return value; if (value.respondsTo("to_ary")) { return TypeConverter.convertToType(context, value, context.runtime.getArray(), "to_ary", false); } return context.runtime.newArray(value); }
public static IRubyObject convertToType(ThreadContext context, IRubyObject obj, RubyClass target, String convertMethod, boolean raise) { IRubyObject r = obj.checkCallMethod(context, convertMethod); return r == null ? handleUncoercibleObject(context.runtime, obj, target, raise) : r; }
public static IRubyObject checkFloatType(Ruby runtime, IRubyObject obj) { if (obj instanceof RubyFloat) return obj; if (!(obj instanceof RubyNumeric)) return runtime.getNil(); return TypeConverter.convertToTypeWithCheck(obj, runtime.getFloat(), "to_f"); }
public static RubyArray rb_Array(ThreadContext context, IRubyObject val) { IRubyObject tmp = checkArrayType(context, val); // to_ary if (tmp == context.nil) { TypeConverterSites sites = sites(context); tmp = convertToTypeWithCheck(context, val, context.runtime.getArray(), sites.to_a_checked); if (tmp == context.nil) { return context.runtime.newArray(val); } } return (RubyArray) tmp; }