From: Robert Klemme Date: 2007-05-21T23:35:05+09:00 Subject: Re: using Enumerable when each has arguments? On 21.05.2007 15:27, Trans wrote: > > On May 21, 6:41 am, "Erwin Abbott" wrote: >> Hi >> >> I've written a number of classes which would benefit from the >> Enumerable mixin, but my #each method requires arguments. These are >> usually to specify the range of values, like Fibonacci#each(first, >> last) or whatever. Since Enumerable's #map, #collect, etc don't take >> arguments, how should I proceed? I would be okay with wrapping the >> Enumerable methods I need, but that doesn't seem possible. Do I just >> have to implement my own #map, #to_a, etc? > > The standard response is "use enumerator and to_enum". but if you feel > like me, that's equivalent to tying knots in your spaghetti noodles to > save money on rotini. I on the other hand am a big fan of Enumerator. :-) > I heard rumor that a future Ruby will ultimately pass thru each > parameters, but in the mean time you can have a go with Facets > enumerablepass.rb (http://facets.rubyforge.org). That's a good solution. > An alternative is to use a functor (a function delegator) for your > parameters, eg. > > f = Foo.new > f.range(1,5).each{|x| p x} ... which is basically the same as using to_enum - only less portable; to_enum works with *all* methods. :-) Kind regards robert