Simon Kröger
9/23/2007 10:39:00 AM
Rick DeNatale schrieb:
> On 9/22/07, gabriele renzi <rff_rffREMOVE@yahoo.it> 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