#-- # Author:: Tyler Rick # Copyright:: Copyright (c) 2007 QualitySmith, Inc. # License:: Ruby License # Submit to Facets?:: No. # Deprecated. Ruby 1.9 implements #select correctly. #++ require "rubygems" if RUBY_VERSION < '1.9' class Hash # call-seq: # hash.hash_select {| key, value | block } -> hash # # Hash#reject returns a hash. One would intuitively expect Hash#select to also return a hash. However, it doesn't: instead, returns "a new array consisting of [key,value] pairs for which the block returns true". # # Hash#hash_select behaves how Hash#select (arguably) _should_ behave: Deletes every key-value pair from a copy of hash _except_ those for which block evaluates to true. # def hash_select(&block) reject {|k, v| !yield k, v } end alias_method :hash_find_all, :hash_select alias_method :delete_unless, :hash_select end end # _____ _ # |_ _|__ ___| |_ # | |/ _ \/ __| __| # | | __/\__ \ |_ # |_|\___||___/\__| # =begin test require 'test/unit' class HashSelectTest < Test::Unit::TestCase def test_1 hash_copy = hash = {:a => 1, :b => 2} assert_equal hash.reject {|k,v| k != :b}, hash.hash_select {|k,v| k == :b} assert_equal(hash_copy, hash) end end =end