From: Jeff Nyman Date: 2006-10-04T02:15:09+09:00 Subject: Re: Using Each to Iterate [Sending this from another account, so I'm sorry if this double-posts.] With all the responses I got here, I figured it out. Thanks to all of you. I'll put the relevant source here and what I did, just in case this helps others. =============== First, a bit of explanation. I have a Step class. I also have a StepList class. (This is similar to the Song and SongList class in the Pickaxe book.) The idea is that when a new step is created, I do so like this: $stepList.append(Step.new(firstStep, thisStep, thisFilter)) So here are the classes: class Step attr_reader :point1, :point2, :filter def initialize(point1, point2, filter=nil) @point1 = point1 @point2 = point2 @filter = filter end end class StepList def initialize @guides = Array.new end def append(thisGuide) @guides.push(thisGuide) self end def filter(thisGuide) @guides.find { |guide| thisGuide == guide.filter } end def [](index) @guides[index] if index.kind_of?(Integer) end def each @guides.find { |guide| guide.filter } end end Then I have $stepList referencing a StepList object. When I have added all the steps and just do a print of $stepList, I get this: #, #, #]> So what I wanted to do is filter through all the Step objects that are in the StepList and then check if they have a filter set. So here was the logic I had: $stepList.each { |thisFilter| puts thisFilter } What my hope was is that the "each" method of my StepList class would return me the filters. And it did -- if I changed my method like this: def each @guides.find { |guide| puts guide.filter } end (or if I used "yield" as some had suggested). With the puts or yield in place, I saw this when the code for the $stepList.each is run: first after 202G_OrdAdd,203G_OrdUpdateFirst last,203G_OrdUpdateLast last,203G_OrdUpdateLast Those are definitely the filters. So what I was hoping was that each of those would, in turn, get sent to thisFilter in my each loop. That appeared to not happen. If I changed the 'each' method to a 'selection' method (Paul's idea), I got this if I had yield in place in the method: ./classes.rb:118:in `selection': no block given (LocalJumpError) However, if I did not have yield in place, I got this: undefined method `each' for # That seemed to make sense since I needed an 'each' method for my Step class. So I added this 'each' method to the Step class: def each yield @filter end That seems to do the trick perfectly. - Jeff