From: Matt Mencel Date: 2012-05-17T06:30:04+09:00 Subject: Re: How to Iterate Through a Class Yup that worked.... Thanks, Matt ----- Original Message ----- From: "Martin DeMello" To: "ruby-talk ML" Sent: Wednesday, May 16, 2012 4:25:08 PM Subject: Re: How to Iterate Through a Class do you want to find all objects that belong to that class? you need to use ObjectSpace for that: ObjectSpace.each_object(classname) {|obj| do something} the .each method in your class is for individual objects to provide internal iterators (e.g. if you're implementing a collection class) martin On Wed, May 16, 2012 at 2:16 PM, Matt Mencel wrote: > How do I iterate through a Class so that I can perform some action on each object?  For example... > > MyClass.each do {|obj| DO SOME STUFF} > > > It looks like I have to create the .each method in my class.  I tried something like what's mentioned here... > http://stackoverflow.com/questions/2080007/how-do-i-add-each-method-to-ruby-object-or-should-i-extend-array > > ...but haven't gotten it to work. > > Matt >