CannotConvertTypeToTargetType() public static method

public static CannotConvertTypeToTargetType ( RubyContext context, object param, string toType ) : Exception
context RubyContext
param object
toType string
return System.Exception
コード例 #1
0
ファイル: Protocols.cs プロジェクト: gaybro8777/ironruby
        /// <summary>
        /// Convert to a Float, using to_f
        /// Throws if conversion fails
        /// </summary>
        public static double ConvertToFloat(RubyContext /*!*/ context, object value)
        {
            if (value == null)
            {
                throw RubyExceptions.CreateTypeError("can't convert nil into Float");
            }
            if (value is int || value is double)
            {
                return(Converter.ConvertToDouble(value));
            }
            if (value is BigInteger)
            {
                return(((BigInteger)value).ToFloat64());
            }
            if (value is MutableString)
            {
                return(ConvertStringToFloat(context, (MutableString)value));
            }

            if (RubySites.RespondTo(context, value, "to_f"))
            {
                object obj = _ToF.Target(_ToF, context, value);
                if (!(obj is double))
                {
                    throw RubyExceptions.MethodShouldReturnType(context, value, "to_f", "Float");
                }
                return((double)obj);
            }

            throw RubyExceptions.CannotConvertTypeToTargetType(context, value, "Float");
        }
コード例 #2
0
ファイル: Protocols.cs プロジェクト: gaybro8777/ironruby
        /// <summary>
        /// Standard way to convert to a Ruby Integer, using to_int and to_i
        /// Trys to call to_int, followed by to_i (if implemented).
        /// If neither is callable, throws a type error.
        /// </summary>
        public static void ConvertToInteger(RubyContext /*!*/ context, object obj, out int fixnum, out BigInteger bignum)
        {
            // Don't call to_int, to_i on primitive types:
            if (AsPrimitiveInteger(obj, out fixnum, out bignum))
            {
                return;
            }

            if (RubySites.RespondTo(context, obj, "to_int"))
            {
                object result = _ToInt.Target(_ToInt, context, obj);
                if (AsPrimitiveInteger(result, out fixnum, out bignum))
                {
                    return;
                }

                throw RubyExceptions.MethodShouldReturnType(context, obj, "to_int", "Integer");
            }

            if (RubySites.RespondTo(context, obj, "to_i"))
            {
                object result = _ToI.Target(_ToI, context, obj);
                if (AsPrimitiveInteger(result, out fixnum, out bignum))
                {
                    return;
                }

                throw RubyExceptions.MethodShouldReturnType(context, obj, "to_i", "Integer");
            }

            throw RubyExceptions.CannotConvertTypeToTargetType(context, obj, "Integer");
        }
コード例 #3
0
ファイル: Protocols.cs プロジェクト: gaybro8777/ironruby
        /// <summary>
        /// Try to cast the object to an Integer using to_int
        /// Throws if the cast fails
        /// Can return either Bignum or Fixnum
        /// </summary>
        public static void CastToInteger(RubyContext /*!*/ context, object obj, out int fixnum, out BigInteger bignum)
        {
            if (AsInteger(context, obj, out fixnum, out bignum))
            {
                return;
            }

            throw RubyExceptions.CannotConvertTypeToTargetType(context, obj, "Integer");
        }
コード例 #4
0
ファイル: Protocols.cs プロジェクト: gaybro8777/ironruby
        /// <summary>
        /// Standard way to convert to a Ruby String, using to_str
        ///
        /// Checks if it's already a string, and if so returns it.
        /// Then calls to_str if it exists, otherwise throw a TypeError
        /// </summary>
        public static MutableString /*!*/ CastToString(RubyContext /*!*/ context, object obj)
        {
            MutableString str = AsString(context, obj);

            if (str != null)
            {
                return(str);
            }
            throw RubyExceptions.CannotConvertTypeToTargetType(context, obj, "String");
        }
コード例 #5
0
ファイル: Protocols.cs プロジェクト: gaybro8777/ironruby
        /// <summary>
        /// Works like AsArray, but throws a type error if the conversion fails
        /// </summary>
        public static IList /*!*/ CastToArray(RubyContext /*!*/ context, object obj)
        {
            IList ary = AsArray(context, obj);

            if (ary != null)
            {
                return(ary);
            }

            throw RubyExceptions.CannotConvertTypeToTargetType(context, obj, "Array");
        }