Ejemplo n.º 1
0
        /// <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");
        }
Ejemplo n.º 2
0
        /// <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");
        }
Ejemplo n.º 3
0
        public IOWrapper(RubyContext /*!*/ context, object /*!*/ obj, FileAccess access)
        {
            _context = context;
            _obj     = obj;

            if (access == FileAccess.Read || access == FileAccess.ReadWrite)
            {
                _canRead = RubySites.RespondTo(context, obj, "read");
            }
            else
            {
                _canRead = false;
            }

            if (access == FileAccess.Write || access == FileAccess.ReadWrite)
            {
                _canWrite = RubySites.RespondTo(context, obj, "write");
            }
            else
            {
                _canWrite = false;
            }

            _canSeek = (RubySites.RespondTo(context, obj, "seek") && RubySites.RespondTo(context, obj, "tell"));

            _buffer   = new byte[_bufferSize];
            _writePos = 0;
            _readPos  = 0;
            _readLen  = 0;
        }
        private object RequireWriteProtocol(RubyContext /*!*/ context, object value, string /*!*/ variableName)
        {
            if (!RubySites.RespondTo(context, value, "write"))
            {
                throw RubyExceptions.CreateTypeError(String.Format("${0} must have write method, {1} given", variableName, RubyUtils.GetClassName(context, value)));
            }

            return(value);
        }
Ejemplo n.º 5
0
 public static object Tagurize(RubyContext context, RubyModule self, object arg)
 {
     if (arg == null)
     {
         return(null);
     }
     if (RubySites.RespondTo(context, arg, "to_str"))
     {
         return(MutableString.Create("tag:yaml.org,2002:").Append(Protocols.ConvertToString(context, arg)));
     }
     return(arg);
 }
Ejemplo n.º 6
0
        /// <summary>
        /// Try to cast the object to an Integer using to_int
        /// Returns null if the object doesn't implement to_int
        /// Can return either Bignum or Fixnum
        /// </summary>
        public static bool AsInteger(RubyContext /*!*/ context, object obj, out int fixnum, out BigInteger bignum)
        {
            // Don't call to_int on types derived from Integer
            if (AsPrimitiveInteger(obj, out fixnum, out bignum))
            {
                return(true);
            }

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

                throw RubyExceptions.InvalidValueForType(context, result, "Integer");
            }

            return(false);
        }
Ejemplo n.º 7
0
        /// <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 returns null
        /// </summary>
        public static MutableString AsString(RubyContext /*!*/ context, object obj)
        {
            MutableString str = obj as MutableString;

            if (str != null)
            {
                return(str);
            }
            if (RubySites.RespondTo(context, obj, "to_str"))
            {
                str = _ToStr.Target(_ToStr, context, obj) as MutableString;
                if (str != null)
                {
                    return(str);
                }

                throw RubyExceptions.MethodShouldReturnType(context, obj, "to_str", "String");
            }

            return(null);
        }
Ejemplo n.º 8
0
        /// <summary>
        /// Try to convert obj to an Array using #to_ary
        /// 1. If obj is an Array (or a subtype), returns it
        /// 2. Calls to_ary if it exists, possibly throwing if to_ary doesn't return an Array
        /// 3. else returns null
        /// </summary>
        public static IList AsArray(RubyContext /*!*/ context, object obj)
        {
            // Don't call to_a on types derived from Array
            IList ary = obj as IList;

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

            if (RubySites.RespondTo(context, obj, "to_ary"))
            {
                object result = _ToAry.Target(_ToAry, context, obj);
                ary = result as IList;
                if (ary != null)
                {
                    return(ary);
                }

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

            return(null);
        }