Esempio n. 1
0
 public static Hash /*!*/ InitializeCopy(RubyContext /*!*/ context, Hash /*!*/ self, [NotNull] Hash /*!*/ source)
 {
     self.DefaultProc  = source.DefaultProc;
     self.DefaultValue = source.DefaultValue;
     IDictionaryOps.ReplaceData(self, source);
     return(self);
 }
Esempio n. 2
0
        public static Hash GroupBy(CallSiteStorage <EachSite> /*!*/ each, [NotNull] BlockParam /*!*/ predicate, object self)
        {
            var grouped = new Dictionary <object, object>();

            Each(each, self, Proc.Create(each.Context, delegate(BlockParam /*!*/ selfBlock, object _, object item) {
                object blockResult;
                if (predicate.Yield(item, out blockResult))
                {
                    return(selfBlock.PropagateFlow(predicate, blockResult));
                }

                RubyArray existingGroup = IDictionaryOps.GetElement(each.Context, grouped, blockResult) as RubyArray;
                if (existingGroup != null)
                {
                    existingGroup.Add(item);
                }
                else
                {
                    IDictionaryOps.SetElement(each.Context, grouped, blockResult, new RubyArray {
                        item
                    });
                }

                return(null);
            }));

            return(new Hash(grouped));
        }
Esempio n. 3
0
        public static Hash /*!*/ Replace(RubyContext /*!*/ context, Hash /*!*/ self, object other)
        {
            if (Object.ReferenceEquals(self, other))
            {
                return(self);
            }

            RubyUtils.RequiresNotFrozen(context, self);

            // If we are copying from another Hash, copy the default value/block, otherwise set to nil
            Hash otherHash = other as Hash;

            self.DefaultValue = (otherHash != null) ? otherHash.DefaultValue : null;
            self.DefaultProc  = (otherHash != null) ? otherHash.DefaultProc : null;
            return(IDictionaryOps.ReplaceData(self, IDictionaryOps.ConvertToHash(context, other)));
        }
Esempio n. 4
0
        public static Hash /*!*/ Replace(RubyContext /*!*/ context, Hash /*!*/ self, [DefaultProtocol, NotNull] IDictionary <object, object> /*!*/ other)
        {
            if (Object.ReferenceEquals(self, other))
            {
                self.RequireNotFrozen();
                return(self);
            }

            Hash otherHash = other as Hash;

            if (otherHash != null)
            {
                self.DefaultValue = otherHash.DefaultValue;
                self.DefaultProc  = otherHash.DefaultProc;
            }
            return(IDictionaryOps.ReplaceData(self, other));
        }
Esempio n. 5
0
        public static object Shift(RubyContext /*!*/ context, Hash /*!*/ self)
        {
            RubyUtils.RequiresNotFrozen(context, self);

            if (self.Count == 0)
            {
                return(_DefaultSite.Target(_DefaultSite, context, self, null));
            }

            IEnumerator <KeyValuePair <object, object> > e = self.GetEnumerator();

            e.MoveNext();
            KeyValuePair <object, object> pair = e.Current;

            self.Remove(pair.Key);

            return(IDictionaryOps.MakeArray(pair));
        }
Esempio n. 6
0
        public static object Shift(CallSiteStorage <Func <CallSite, Hash, object, object> > /*!*/ storage, Hash /*!*/ self)
        {
            self.RequireNotFrozen();

            if (self.Count == 0)
            {
                var site = storage.GetCallSite("default", 1);
                return(site.Target(site, self, null));
            }

            IEnumerator <KeyValuePair <object, object> > e = self.GetEnumerator();

            e.MoveNext();
            KeyValuePair <object, object> pair = e.Current;

            self.Remove(pair.Key);

            return(IDictionaryOps.MakeArray(pair));
        }
Esempio n. 7
0
        public static Hash /*!*/ CreateHash(RubyClass /*!*/ self, [NotNull] params object[] items)
        {
            // arg0: hash or first elem
            // argk: (k-1)th elem
            int itemCount = items.Length;

            IDictionary <object, object> hash = null;

            if (itemCount == 0 || itemCount == 1 && (hash = items[0] as IDictionary <object, object>) != null)
            {
                Hash newHash = _CreateHashSite.Target(_CreateHashSite, self.Context, self);
                return(hash != null?IDictionaryOps.ReplaceData(newHash, hash) : newHash);
            }

            if (itemCount % 2 != 0)
            {
                throw new ArgumentException("odd number of arguments for Hash");
            }

            return(RubyUtils.MakeHash(self.Context, items));
        }
Esempio n. 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));
            }
        }
Esempio n. 9
0
 public static Hash /*!*/ CreateSubclass(RubyClass /*!*/ self, [NotNull] IDictionary <object, object> /*!*/ hash)
 {
     // creates a new hash and copies entries of the given hash into it (no other objects associated with the has are copied):
     return(IDictionaryOps.ReplaceData(Hash.CreateInstance(self), hash));
 }
Esempio n. 10
0
 public static MutableString /*!*/ Inspect(RubyContext /*!*/ context, object /*!*/ self)
 {
     return(IDictionaryOps.ToMutableString(context, ToHash(context, self)));
 }
Esempio n. 11
0
 public static Hash /*!*/ Replace(RubyContext /*!*/ context, Hash /*!*/ self, [DefaultProtocol, NotNull] IDictionary <object, object> /*!*/ other)
 {
     self.Mutate();
     return(IDictionaryOps.ReplaceData(self, other));
 }