Beispiel #1
0
        public static object EachObject(BlockParam block, RubyModule /*!*/ self, [NotNull] RubyClass /*!*/ theClass)
        {
            if (!theClass.HasAncestor(self.Context.ModuleClass))
            {
                throw RubyExceptions.CreateRuntimeError("each_object only supported for objects of type Class or Module");
            }

            if (block == null)
            {
                throw RubyExceptions.NoBlockGiven();
            }

            int matches = 0;
            List <RubyModule>  visitedModules = new List <RubyModule>();
            Stack <RubyModule> pendingModules = new Stack <RubyModule>();

            pendingModules.Push(theClass.Context.ObjectClass);

            while (pendingModules.Count > 0)
            {
                RubyModule next = pendingModules.Pop();
                visitedModules.Add(next);

                if (theClass.Context.IsKindOf(next, theClass))
                {
                    matches++;

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

                using (theClass.Context.ClassHierarchyLocker()) {
                    next.EnumerateConstants(delegate(RubyModule module, string name, object value) {
                        RubyModule constAsModule = value as RubyModule;
                        if (constAsModule != null && !visitedModules.Contains(constAsModule))
                        {
                            pendingModules.Push(constAsModule);
                        }
                        return(false);
                    });
                }
            }
            return(matches);
        }
Beispiel #2
0
 internal override bool IsSuperClassAncestor(RubyModule /*!*/ module)
 {
     return(_superClass != null && _superClass.HasAncestor(module));
 }