From: Robert Klemme Date: 2011-08-02T16:30:38+09:00 Subject: Re: system stack error in ObjectSpace.each_object luke gruber wrote in post #1014312: >> I assume you created some weird kind of recursion via >> define_finalizer. I'm not really sure what's going on. Can you >> explain what your code is supposed to do? > Yeah, it was just for fun and to learn some of these metaprogramming > methods, but what I was meaning to do was to be able to make a 'leader' > object of, say, class Foo. Upon choosing the leader object, you have to > choose a subordinate class to lead (which could be the same class as the > leader object, ie: Foo. > > Basically the implementation was to take all the objects of the > subordinate class, then override all of the instance methods in the > leader object's class by putting new methods in the leader's singleton > class with the same name as the instance methods in the leader's class. > Every time these new 'leader' methods are called (ie: the leader > responds to a method that's defined in the leader's own class), it sends > the same method (with the same args &block) to all the subordinate > objects. Not sure what this is supposed to do but I would always make the connection between those objects explicit. And I would separate the leading from determining the set of instances to lead. Here's one way class Leader < BasicObject def lead(source) @objs = source end def method_missing(*a,&b) @objs.map {|o| o.send(*a,&b)} end end class Module Source = Struct.new :cl do include Enumerable def each(&b) return to_enum(:each) unless b ObjectSpace.each_object(cl, &b) self end end def lemmings Source.new self end end l = Leader.new l.lead Hash.lemmings p l.size Kind regards robert -- Posted via http://www.ruby-forum.com/.