Пример #1
0
 /// <summary>
 /// Check that the object, when converted to an integer, is not less than or equal to zero.
 /// </summary>
 private static void CheckStep(RubyContext /*!*/ context, object step)
 {
     if (RubySites.Equal(context, step, 0))
     {
         throw RubyExceptions.CreateArgumentError("step can't be 0");
     }
     if (RubyOps.IsTrue(LibrarySites.LessThan(context, step, 0)))
     {
         throw RubyExceptions.CreateArgumentError("step can't be negative");
     }
 }
Пример #2
0
        private void AppendInspect()
        {
            MutableString inspect = RubySites.Inspect(_context, _opts.Value);

            if (KernelOps.Tainted(_context, inspect))
            {
                _tainted = true;
            }

            AppendString(inspect);
        }
Пример #3
0
            public static MutableString Inspect(RubyContext /*!*/ context, FileSystemInfo /*!*/ self)
            {
                string result = String.Format(
                    "#<File::Stat dev={0}, ino={1}, mode={2}, nlink={3}, uid={4}, gid={5}, rdev={6}, size={7}, blksize={8}, blocks={9}, atime={10}, mtime={11}, ctime={12}",
                    RubySites.Inspect(context, DeviceId(self)), RubySites.Inspect(context, Inode(self)), RubySites.Inspect(context, Mode(self)),
                    RubySites.Inspect(context, NumberOfLinks(self)), RubySites.Inspect(context, UserId(self)), RubySites.Inspect(context, GroupId(self)),
                    RubySites.Inspect(context, DeviceId(self)), RubySites.Inspect(context, Size(self)), RubySites.Inspect(context, BlockSize(self)),
                    RubySites.Inspect(context, Blocks(self)), TimeOps.ToString(AccessTime(self)), TimeOps.ToString(ModifiedTime(self)),
                    TimeOps.ToString(CreateTime(self))
                    );

                return(MutableString.Create(result));
            }
Пример #4
0
        /// <summary>
        /// Step through a Range of Strings.
        /// </summary>
        /// <remarks>
        /// This method requires step to be a Fixnum.
        /// It uses a hybrid string comparison to prevent infinite loops and calls String#succ to get each item in the range.
        /// </remarks>
        private static object StepString(RubyContext /*!*/ context, BlockParam block, Range /*!*/ self, MutableString begin, MutableString end, int step)
        {
            CheckStep(step);
            object        result;
            MutableString item = begin;
            int           comp;

            while ((comp = Protocols.Compare(context, item, end)) < 0)
            {
                if (block == null)
                {
                    throw RubyExceptions.NoBlockGiven();
                }

                if (block.Yield(item, out result))
                {
                    return(result);
                }

                for (int i = 0; i < step; ++i)
                {
                    item = (MutableString)RubySites.Successor(context, item);
                }

                if (item.Length > end.Length)
                {
                    return(self);
                }
            }

            if (comp == 0 && !self.ExcludeEnd)
            {
                if (block == null)
                {
                    throw RubyExceptions.NoBlockGiven();
                }

                if (block.Yield(item, out result))
                {
                    return(result);
                }
            }
            return(self);
        }
Пример #5
0
        public static bool Equals(RubyStruct /*!*/ self, object obj)
        {
            var other = obj as RubyStruct;

            if (!self.StructReferenceEquals(other))
            {
                return(false);
            }

            Debug.Assert(self.ItemCount == other.ItemCount);
            for (int i = 0; i < self.Values.Length; i++)
            {
                if (!RubySites.Equal(self.Class.Context, self.Values[i], other.Values[i]))
                {
                    return(false);
                }
            }

            return(true);
        }
Пример #6
0
        public static RubyArray Grep(RubyContext /*!*/ context, BlockParam action, object self, object pattern)
        {
            RubyArray result = new RubyArray();

            Each(context, self, Proc.Create(context, delegate(BlockParam /*!*/ selfBlock, object item) {
                if (RubySites.CaseEqual(context, pattern, item))
                {
                    if (action != null)
                    {
                        if (action.Yield(item, out item))
                        {
                            return(item);
                        }
                    }
                    result.Add(item);
                }
                return(null);
            }));

            return(result);
        }
Пример #7
0
        public static object Equal(RubyContext /*!*/ context, object self, object other)
        {
            // Short circuit long winded comparison if the objects are actually the same.
            if (self == other)
            {
                return(true);
            }

            // TODO: handle exceptions thrown from Compare()
            // Compare may return null
            object result = RubySites.Compare(context, self, other);

            if (result is int)
            {
                return((int)result == 0);
            }
            else
            {
                return(null);
            }
        }
Пример #8
0
        public static MutableString /*!*/ ToPrintedString(RubyContext /*!*/ context, object obj)
        {
            IDictionary <object, object> hash;
            List <object> list;
            MutableString str;

            if ((list = obj as List <object>) != null)
            {
                return(IListOps.Join(context, list, Environment.NewLine));
            }
            else if ((hash = obj as IDictionary <object, object>) != null)
            {
                return(IDictionaryOps.ToString(context, hash));
            }
            else if (obj == null)
            {
                return(MutableString.Create("nil"));
            }
            else if (obj is bool)
            {
                return(MutableString.Create((bool)obj ? "true" : "false"));
            }
            else if (obj is double)
            {
                var result = MutableString.Create(obj.ToString());
                if ((double)(int)(double)obj == (double)obj)
                {
                    result.Append(".0");
                }
                return(result);
            }
            else if ((str = obj as MutableString) != null)
            {
                return(str);
            }
            else
            {
                return(RubySites.ToS(context, obj));
            }
        }
Пример #9
0
 public static MutableString Inspect(RubyContext /*!*/ context, IDictionary <object, object> /*!*/ self)
 {
     using (IDisposable handle = RubyUtils.InfiniteInspectTracker.TrackObject(self)) {
         if (handle == null)
         {
             return(MutableString.Create("{...}"));
         }
         MutableString str = MutableString.CreateMutable();
         str.Append('{');
         foreach (KeyValuePair <object, object> pair in self)
         {
             if (str.Length != 1)
             {
                 str.Append(", ");
             }
             str.Append(RubySites.Inspect(context, BaseSymbolDictionary.ObjToNull(pair.Key)));
             str.Append("=>");
             str.Append(RubySites.Inspect(context, pair.Value));
         }
         str.Append('}');
         return(str);
     }
 }
Пример #10
0
        /// <summary>
        /// Step through a Range of objects that are not Numeric or String.
        /// </summary>
        private static object StepObject(RubyContext /*!*/ context, BlockParam block, Range /*!*/ self, object begin, object end, int step)
        {
            CheckStep(context, step);
            object item = begin, result;
            int    comp;

            while ((comp = Protocols.Compare(context, item, end)) < 0)
            {
                if (block == null)
                {
                    throw RubyExceptions.NoBlockGiven();
                }

                if (block.Yield(item, out result))
                {
                    return(result);
                }

                for (int i = 0; i < step; ++i)
                {
                    item = RubySites.Successor(context, item);
                }
            }
            if (comp == 0 && !self.ExcludeEnd)
            {
                if (block == null)
                {
                    throw RubyExceptions.NoBlockGiven();
                }

                if (block.Yield(item, out result))
                {
                    return(result);
                }
            }
            return(self);
        }
Пример #11
0
        public static object Find(RubyContext /*!*/ context, BlockParam predicate, object self, [Optional] object ifNone)
        {
            object result = Missing.Value;

            Each(context, self, Proc.Create(context, delegate(BlockParam /*!*/ selfBlock, object item) {
                if (predicate == null)
                {
                    throw RubyExceptions.NoBlockGiven();
                }

                object blockResult;
                if (predicate.Yield(item, out blockResult))
                {
                    result = blockResult;
                    return(selfBlock.Break(blockResult));
                }

                if (Protocols.IsTrue(blockResult))
                {
                    result = item;
                    return(selfBlock.Break(item));
                }

                return(null);
            }));

            if (result == Missing.Value)
            {
                if (ifNone == Missing.Value || ifNone == null)
                {
                    return(null);
                }
                result = RubySites.Call(context, ifNone);
            }
            return(result);
        }
Пример #12
0
 public static object InducedFrom(RubyClass /*!*/ self, double obj)
 {
     return(RubySites.ToI(self.Context, obj));
 }
Пример #13
0
 public static double InducedFrom(RubyClass /*!*/ self, [NotNull] BigInteger /*!*/ value)
 {
     return(RubySites.ToF(self.Context, value));
 }
Пример #14
0
 public static double InducedFrom(RubyClass /*!*/ self, int value)
 {
     return(RubySites.ToF(self.Context, value));
 }
Пример #15
0
 internal string /*!*/ GetDebugView()
 {
     return(RubySites.Inspect(RubyContext._Default, this).ToString());
 }
Пример #16
0
 public static object ToInt(RubyContext /*!*/ context, object self)
 {
     return(RubySites.ToI(context, self));
 }