Beispiel #1
0
 public static double ConvertStringToFloat(RubyContext context, MutableString value)
 {
     try {
         return(double.Parse(value.ConvertToString(), System.Globalization.CultureInfo.InvariantCulture.NumberFormat));
     } catch (FormatException x) {
         MutableString valueString = RubySites.Inspect(context, value);
         throw RubyExceptions.CreateArgumentError("invalid value for Float(): " + valueString.ConvertToString(), x);
     }
 }
Beispiel #2
0
            public static MutableString Inspect(RubyContext /*!*/ context, YamlStream /*!*/ self)
            {
                MutableString result = MutableString.CreateMutable("#<YAML::Stream:");

                RubyUtils.AppendFormatHexObjectId(result, RubyUtils.GetObjectId(context, self))
                .Append(" @documents=")
                .Append(RubySites.Inspect(context, self.Documents))
                .Append(", options=")
                .Append(RubySites.Inspect(context, self.Options))
                .Append('>');
                return(result);
            }
Beispiel #3
0
        public static MutableString /*!*/ ObjectToMutableString(RubyContext /*!*/ context, object obj)
        {
            using (IDisposable handle = RubyUtils.InfiniteInspectTracker.TrackObject(obj)) {
                if (handle == null)
                {
                    return(MutableString.Create("..."));
                }

                MutableString str = MutableString.CreateMutable();
                str.Append("#<");
                str.Append(context.GetClassOf(obj).Name);

                // Ruby prints 2*object_id for objects
                str.Append(':');
                AppendFormatHexObjectId(str, GetObjectId(context, obj));

                // display instance variables
                RubyInstanceData data = context.TryGetInstanceData(obj);
                if (data != null)
                {
                    var  vars  = data.GetInstanceVariablePairs();
                    bool first = true;
                    foreach (KeyValuePair <string, object> var in vars)
                    {
                        if (first)
                        {
                            str.Append(" ");
                            first = false;
                        }
                        else
                        {
                            str.Append(", ");
                        }
                        str.Append(var.Key);
                        str.Append("=");
                        str.Append(RubySites.Inspect(context, var.Value));
                    }
                }
                str.Append(">");

                return(str);
            }
        }
Beispiel #4
0
        /// <summary>
        /// Casts to symbol. Note that this doesn't actually use to_sym -- it uses to_str.
        /// That's just how Ruby does it.
        ///
        /// Another fun detail: you can pass Fixnums as Symbols. If you pass a Fixnum that
        /// doesn't map to a Symbol (i.e. Fixnum#to_sym returns nil), you get an ArgumentError
        /// instead of a TypeError. At least it produces a warning about using Fixnums as Symbols
        /// </summary>
        public static string /*!*/ CastToSymbol(RubyContext /*!*/ context, object obj)
        {
            if (obj is SymbolId)
            {
                return(SymbolTable.IdToString((SymbolId)obj));
            }

            if (obj is int)
            {
                return(RubyOps.ConvertFixnumToSymbol(context, (int)obj));
            }
            else
            {
                MutableString str = AsString(context, obj);
                if (str != null)
                {
                    return(RubyOps.ConvertMutableStringToSymbol(str));
                }
            }

            throw RubyExceptions.CreateTypeError(String.Format("{0} is not a symbol", RubySites.Inspect(context, obj)));
        }
Beispiel #5
0
 public static Node ToYaml(object self, [NotNull] RubyRepresenter /*!*/ rep)
 {
     return(rep.Scalar(self, RubySites.Inspect(rep.Context, self)));
 }
Beispiel #6
0
 public static Exception /*!*/ InvalidValueForType(RubyContext /*!*/ context, object obj, string type)
 {
     return(CreateArgumentError(String.Format("invalid value for {0}: {1}", type, RubySites.Inspect(context, obj).ConvertToString())));
 }