From: Robert Klemme Date: 2008-01-30T07:30:18+09:00 Subject: Re: neat idea from arc On 29.01.2008 23:07, Martin DeMello wrote: > Paul Graham just released Arc (http://paulgraham.com/arc0.html), a > lisp dialect with an emphasis on "quick-and-dirty" exploratory > programming, and compactness of code. One of the ideas he threw in > seems like it might fit well into Ruby too - when filtering functions > like keep and rem (select and reject) are given a value rather than a > function, they implicitly construct the equality function and pass > that in instead. The ruby equivalent would be to have select, reject, > partition, any? and all? (and possibly some others I'm overlooking) do > the following: > > def select(arg, &blk) > if block_given? > old_select(&blk) > else > old_select {|x| arg === x} > end > end You either need "*arg" or "arg = nil" because otherwise the method cannot be invoked without arg. Here's another way to write it: module Enumerable def select2(a = nil, &b) select(&(b || lambda {|e| a === e})) end end but I guess your variant is more efficient. > and likewise for the others > > So we can write, for instance > > ints = input.select Integer > > lcase = words.reject /[A-Z]/ > > and so on. What say you? Sounds good! Btw, there is already #grep which works like the new #select with argument but I agree, the idea to put this in one method sounds better. Kind regards robert