On Thu, Jul 12, 2007 at 06:34:31AM +0900, Stefan Rusterholz wrote:
> Ezra Zygmuntowicz wrote:
> >   Here are two handy methods for doing what you want:
> > 
> > class Hash
> >    # lets through the keys in the argument
> >    # >> {:one => 1, :two => 2, :three => 3}.pass(:one)
> >    # => {:one=>1}
> >    def pass(*keys)
> >      self.reject { |k,v| ! keys.include?(k) }
> >    end
> > 
> >    # blocks the keys in the arguments
> >    # >> {:one => 1, :two => 2, :three => 3}.block(:one)
> >    # => {:two=>2, :three=>3}
> >    def block(*keys)
> >      self.reject { |k,v| keys.include?(k) }
> >    end
> > end
> 
> I wonder, are you aware that those are O(n^2)?

Actually, they are O(n*m) since the size of the arguments array is (almost
certainly) different from and smaller than the size of the hash. That said,
if you really want to make it O(n+m) (or so, and that's a + instead of a
*) you put the arguments list in a hash (which makes the include? call
O(1) instead of O(m)). That probably isn't a win until m is more than three
(or maybe more, it would require benchmarking to find the magic number),
though.

> Regards
> Stefan
--Greg