From: Joel VanderWerf Date: 2003-05-12T06:14:03+09:00 Subject: Re: [RCR?] Enumerable#each with arguments ts wrote: >>>>>>"J" == Joel VanderWerf writes: > > > J> I don't understand. Neither of your #each methods takes arguments. > > You have found the fix :-) > > J> In the case of instances of REXML::Elements#each, an optional XPath string. > > and if someone want to call it with 2, 3, ... arguments ? > > For me, your problem seems specific to a class (REXML::Elements) There's another example, and it's in Ruby itself: ObjectSpace. It's always puzzled me that ObjectSpace doesn't inherit from Enumerable, so you can't collect, select, etc. So why not do this: class << ObjectSpace include Enumerable alias each each_object end module Enumerable def collect(*args, &block) rslt = [] if block each(*args) {|x| rslt << yield(x)} else each(*args) {|x| rslt << x} end rslt end end p ObjectSpace.collect(Class) {|c|c.name}.sort[0..9] The last line is a little bit easier than what works now: tmp = [] ObjectSpace.each_object(Class) {|c| tmp << c.name} p tmp.sort[0..9] Which one is more ruby-like?