From: 7stud -- Date: 2007-09-24T10:01:26+09:00 Subject: Re: How would you write it vs Smalltalk And this seems conceptually easier and it's slightly faster(if I benchmarked it correctly): input = gets h = Hash.new(0) input.scan(/[a-z]/){|char| h[char] += 1} p h.sort ============ require "benchmark" input = "123Hello+#" h = Hash.new(0) Benchmark.bm(20) do |br| br.report("scan with block:") do 200_000.times do input.downcase.scan(/[a-z]/){|char| h[char] += 1} end end end p h.sort puts h = Hash.new(0) Benchmark.bm(20) do |br| br.report("scan with inject:") do 200_000.times do input.downcase.scan(/[a-z]/).inject(h) do |h, c| h[c] += 1 h end end end end p h.sort --output:-- user system total real scan with block: 2.690000 0.010000 2.700000 ( 2.714077) [["e", 200000], ["h", 200000], ["l", 400000], ["o", 200000]] user system total real scan with inject: 2.830000 0.020000 2.850000 ( 2.856019) [["e", 200000], ["h", 200000], ["l", 400000], ["o", 200000]] -- Posted via http://www.ruby-forum.com/.