@JRubyMethod(name = "String", required = 1, module = true, visibility = PRIVATE, compat = RUBY1_9) public static IRubyObject new_string19(ThreadContext context, IRubyObject recv, IRubyObject object) { return TypeConverter.convertToType19(object, context.runtime.getString(), "to_s"); }
@JRubyMethod(name = "String", required = 1, module = true, visibility = PRIVATE, compat = RUBY1_9) public static IRubyObject new_string19(ThreadContext context, IRubyObject recv, IRubyObject object) { return TypeConverter.convertToType19(object, context.runtime.getString(), "to_s"); }
public static RubyArray convertToRubyArrayWithCoerce19(Ruby runtime, IRubyObject value) { if (value instanceof RubyArray) return ((RubyArray)value); IRubyObject newValue = TypeConverter.convertToType19(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; }
public static RubyArray convertToRubyArrayWithCoerce19(Ruby runtime, IRubyObject value) { if (value instanceof RubyArray) return ((RubyArray)value); IRubyObject newValue = TypeConverter.convertToType19(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; }
public static RubyNumeric toFloat(Ruby runtime, IRubyObject obj) { RubyClass floatClass = runtime.getFloat(); if (floatClass.isInstance(obj)) return (RubyNumeric) obj; if (!runtime.getNumeric().isInstance(obj)) throw runtime.newTypeError(obj, "Float"); return (RubyNumeric) convertToType19(obj, floatClass, "to_f", true); } /**
public static RubyNumeric toFloat(Ruby runtime, IRubyObject obj) { RubyClass floatClass = runtime.getFloat(); if (floatClass.isInstance(obj)) return (RubyNumeric) obj; if (!runtime.getNumeric().isInstance(obj)) throw runtime.newTypeError(obj, "Float"); return (RubyNumeric) convertToType19(obj, floatClass, "to_f", true); } /**
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); }
/** * Higher level conversion utility similar to convertToType but it can throw an * additional TypeError during conversion (MRI: rb_check_convert_type). * * @param obj the object to convert * @param targetType is the type we are trying to convert to * @param convertMethodIndex the fast index to use for calling the method * @param convertMethod is the method to be called to try and convert to targeType * @return the converted value */ public static final IRubyObject convertToTypeWithCheck19(IRubyObject obj, RubyClass target, String convertMethod) { if (target.isInstance(obj)) return obj; IRubyObject val = TypeConverter.convertToType19(obj, target, convertMethod, false); if (val.isNil()) return val; if (!target.isInstance(val)) throw obj.getRuntime().newTypeError(obj.getMetaClass() + "#" + convertMethod + " should return " + target.getName()); return val; }
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); }
/** * Higher level conversion utility similar to convertToType but it can throw an * additional TypeError during conversion (MRI: rb_check_convert_type). * * @param obj the object to convert * @param targetType is the type we are trying to convert to * @param convertMethodIndex the fast index to use for calling the method * @param convertMethod is the method to be called to try and convert to targeType * @return the converted value */ public static final IRubyObject convertToTypeWithCheck19(IRubyObject obj, RubyClass target, String convertMethod) { if (target.isInstance(obj)) return obj; IRubyObject val = TypeConverter.convertToType19(obj, target, convertMethod, false); if (val.isNil()) return val; if (!target.isInstance(val)) throw obj.getRuntime().newTypeError(obj.getMetaClass() + "#" + convertMethod + " should return " + target.getName()); return val; }
/** * Converts this object to type 'targetType' using 'convertMethod' method and raises TypeError exception on failure (MRI: rb_convert_type in 1.9). * * @param obj the object to convert * @param targetType is the type we are trying to convert to * @param convertMethod is the method to be called to try and convert to targeType * @return the converted value */ public static final IRubyObject convertToType19(IRubyObject obj, RubyClass target, String convertMethod) { if (target.isInstance(obj)) return obj; IRubyObject val = convertToType19(obj, target, convertMethod, true); if (!target.isInstance(val)) { String cname = obj.getMetaClass().toString(); throw obj.getRuntime().newTypeError("can't convert " + cname + " to " + target.getName() + " (" + cname + "#" + convertMethod + " gives " + val.getMetaClass() + ")"); } return val; }
/** * Converts this object to type 'targetType' using 'convertMethod' method and raises TypeError exception on failure (MRI: rb_convert_type in 1.9). * * @param obj the object to convert * @param targetType is the type we are trying to convert to * @param convertMethod is the method to be called to try and convert to targeType * @return the converted value */ public static final IRubyObject convertToType19(IRubyObject obj, RubyClass target, String convertMethod) { if (target.isInstance(obj)) return obj; IRubyObject val = convertToType19(obj, target, convertMethod, true); if (!target.isInstance(val)) { String cname = obj.getMetaClass().toString(); throw obj.getRuntime().newTypeError("can't convert " + cname + " to " + target.getName() + " (" + cname + "#" + convertMethod + " gives " + val.getMetaClass() + ")"); } return val; }
@JRubyMethod(name = "Float", module = true, visibility = PRIVATE, compat = RUBY1_9) public static RubyFloat new_float19(IRubyObject recv, IRubyObject object) { Ruby runtime = recv.getRuntime(); if(object instanceof RubyFixnum){ return RubyFloat.newFloat(runtime, ((RubyFixnum)object).getDoubleValue()); } else if (object instanceof RubyFloat) { return (RubyFloat)object; } else if(object instanceof RubyBignum){ return RubyFloat.newFloat(runtime, RubyBignum.big2dbl((RubyBignum)object)); } else if(object instanceof RubyString){ if(((RubyString) object).getByteList().getRealSize() == 0){ // rb_cstr_to_dbl case throw runtime.newArgumentError("invalid value for Float(): " + object.inspect()); } RubyString arg = (RubyString)object; if (arg.toString().startsWith("0x")) { return ConvertBytes.byteListToInum19(runtime, arg.getByteList(), 16, true).toFloat(); } return RubyNumeric.str2fnum19(runtime, arg,true); } else if(object.isNil()){ throw runtime.newTypeError("can't convert nil into Float"); } else { return (RubyFloat)TypeConverter.convertToType19(object, runtime.getFloat(), "to_f"); } }
@JRubyMethod(name = "Float", module = true, visibility = PRIVATE, compat = RUBY1_9) public static RubyFloat new_float19(IRubyObject recv, IRubyObject object) { Ruby runtime = recv.getRuntime(); if(object instanceof RubyFixnum){ return RubyFloat.newFloat(runtime, ((RubyFixnum)object).getDoubleValue()); } else if (object instanceof RubyFloat) { return (RubyFloat)object; } else if(object instanceof RubyBignum){ return RubyFloat.newFloat(runtime, RubyBignum.big2dbl((RubyBignum)object)); } else if(object instanceof RubyString){ if(((RubyString) object).getByteList().getRealSize() == 0){ // rb_cstr_to_dbl case throw runtime.newArgumentError("invalid value for Float(): " + object.inspect()); } RubyString arg = (RubyString)object; if (arg.toString().startsWith("0x")) { return ConvertBytes.byteListToInum19(runtime, arg.getByteList(), 16, true).toFloat(); } return RubyNumeric.str2fnum19(runtime, arg,true); } else if(object.isNil()){ throw runtime.newTypeError("can't convert nil into Float"); } else { return (RubyFloat)TypeConverter.convertToType19(object, runtime.getFloat(), "to_f"); } }
IRubyObject kwargsHash = TypeConverter.convertToType19(kwargsVar, runtime.getHash(), "to_hash");
IRubyObject kwargsHash = TypeConverter.convertToType19(kwargsVar, runtime.getHash(), "to_hash");
io1 = (RubyIO) arg1; } else if (arg1.respondsTo("to_path")) { RubyString path = (RubyString) TypeConverter.convertToType19(arg1, runtime.getString(), "to_path"); io1 = (RubyIO) RubyFile.open(context, runtime.getFile(), new IRubyObject[] {path}, Block.NULL_BLOCK); } else if (arg1.respondsTo("read")) { io2 = (RubyIO) arg2; } else if (arg2.respondsTo("to_path")) { RubyString path = (RubyString) TypeConverter.convertToType19(arg2, runtime.getString(), "to_path"); io2 = (RubyIO) RubyFile.open(context, runtime.getFile(), new IRubyObject[] {path, runtime.newString("w")}, Block.NULL_BLOCK); } else if (arg2.respondsTo("write")) {
io1 = (RubyIO) arg1; } else if (arg1.respondsTo("to_path")) { RubyString path = (RubyString) TypeConverter.convertToType19(arg1, runtime.getString(), "to_path"); io1 = (RubyIO) RubyFile.open(context, runtime.getFile(), new IRubyObject[] {path}, Block.NULL_BLOCK); } else if (arg1.respondsTo("read")) { io2 = (RubyIO) arg2; } else if (arg2.respondsTo("to_path")) { RubyString path = (RubyString) TypeConverter.convertToType19(arg2, runtime.getString(), "to_path"); io2 = (RubyIO) RubyFile.open(context, runtime.getFile(), new IRubyObject[] {path, runtime.newString("w")}, Block.NULL_BLOCK); } else if (arg2.respondsTo("write")) {