From: Chris Morris Date: 2002-09-04T02:00:18+09:00 Subject: Re: Ruby aesthetics > I must confess to a lack of Ruby guru-ness. It would be a great tutorial if > someone would be gracious enough to give a token-by-token (blow-by-blow > sounds so violent) commentary on what Reimer just wrote. Don't know if the following is enough commentary, but here goes: > > module Enumerable this module is included in many classes like Array - therefore this new method would automatically be added to every class that includes Enumerable > > def condmap > > result = [] initialize the result to an empty array > > each do call the each method passing the do..end block > > | elem | the element passed into the block from the each iterator > > item = yield elem yield elem to the block passed into condmap. Set item to the result of that block > > result << item unless item.nil? append item to the results array unless item is nil > > end > > result return the result array > > end > > > > end > > > > To be used as follows: > > > > squares_less_than_10 = numbers.condmap{|x| x**2 if x < 10 } Chris http://clabs.org