From: Han Holl Date: 2002-06-09T23:16:52+09:00 Subject: Hash#===() Hi, I ran the following benchmark: require 'benchmark' include Benchmark class Hash def ===(a) has_key?(a) end end n = 100000 hash = { "abc"=> 1, "def"=> 1, "kli"=> 1, "lop"=> 1, "tli"=> 1, "ppp"=> 1 } [ "ppp", "abc", "for" ].each do |a| bm(7) do |x| x.report(a){for i in 1..n; case a when "abc", "def", "kli", "lop", "tli", "ppp" end end} x.report(a){for i in 1..n; case a when /^(abc|def|kli|lop|tli|ppp)$/ end end} x.report(a){for i in 1..n; case a when /^(?:abc|def|kli|lop|tli|ppp)$/ end end} x.report(a){for i in 1..n; case a when hash end end} end end With the following result: user system total real ppp 0.770000 0.000000 0.770000 ( 0.765261) ppp 0.520000 0.000000 0.520000 ( 0.518373) ppp 0.480000 0.000000 0.480000 ( 0.487162) ppp 0.290000 0.000000 0.290000 ( 0.286171) user system total real abc 0.210000 0.000000 0.210000 ( 0.204143) abc 0.440000 0.000000 0.440000 ( 0.443976) abc 0.430000 0.000000 0.430000 ( 0.425147) abc 0.290000 0.000000 0.290000 ( 0.292743) user system total real for 0.780000 0.000000 0.780000 ( 0.776409) for 0.360000 0.000000 0.360000 ( 0.366605) for 0.340000 0.000000 0.340000 ( 0.340457) for 0.260000 0.000000 0.260000 ( 0.258510) This shows that in a case with several alternatives, using a Hash is almost always the best solution. Wouldn't the above definition for Hash#=== make sense as the default ? Cheers, Han Holl