From: Brian Adkins Date: 2007-09-24T13:10:03+09:00 Subject: Re: How would you write it vs Smalltalk On Sep 22, 2:39 pm, "Rick DeNatale" wrote: > On 9/22/07, gabriele renzi wrote: > > > On Sun, 23 Sep 2007 02:37:28 +0900, SpringFlowers AutumnMoon wrote: > > > > I just wonder how would you write it? There is no each_char it seems, > > > unless with ActiveSupport. > > > So something like: > > gets.downcase.each_byte {|b| h[c.chr] += 1 if c =~ ?a..?z} > > Here's another way: > > puts "Enter line:" > h = Hash.new(0) > gets.scan(/[a-zA-Z]/) {|ch| h[ch.downcase] += 1 } > p h.sort More verbose but a teensy bit quicker by avoiding regex & downcase: lambda do |str| h = Hash.new(0) str.each_byte do |b| if (b > 96 && b < 123) h[b.chr] += 1 elsif (b > 64 && b < 91) h[(b+32).chr] += 1 end end h end