From: "Simon Kröger" Date: 2007-09-23T19:40:03+09:00 Subject: Re: How would you write it vs Smalltalk Rick DeNatale schrieb: > 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 Well, we need an inject solution, right? p gets.downcase.scan(/[a-z]/).inject({}){|h,c| h.merge({c,h[c].to_i+1})}.sort cheers Simon