From: Trans Date: 2007-06-22T06:30:40+09:00 Subject: Re: Behaviour of Enumerables reject vs. select mixed into Hash On Jun 21, 4:34 pm, Nobuyoshi Nakada wrote: > Hi, > > At Thu, 21 Jun 2007 23:45:42 +0900, > Trans wrote in [ruby-talk:256401]: > > > I can think of two potential solutions to this issue. Either require a > > method in the enumerated class that dictates how to construct elements > > of that class when enumerated (#<< might do), or we could have two > > separate sets of enumerable methods, one for array elements and one > > for hash key-value pairs. > > Classes which use Enumerable are not only Array and Hash. > What do you expect for IO? Hmm...well for the first solution, I suppose we need a special constructor to provide the kind of enumerable result we will be building. In my example, I used self.class.new, by obviously that's not always the case, so the class will need to tell us. The second solution just has two forms of Enumerable, one which returns an array and the other a hash. So #each_assoc might not alwasy be defined, and thus these "hash-enumerable" methods wouldn't be available. Or it would just deal in pairs, eg. $stdin.select{ |s| s =~ /^x/ } x123 a123 x890 bcde #=> ["x123\n","x890\n"] $stdin.select_assoc{ |k,v| k =~ /^x/ } x123 a123 x890 bcde #=> {"x123\n"=>"a123\n","x890\n"=>"bcde\n"] T.