Hi --

On Mon, 25 Sep 2006, Vimal wrote:

> Hi
> Say I have a Hash, and an instance called wordcount, that maps strings
> to a number count, that counts the occurance of the word.
>
> I do
>
> count = Hash.new
> count[word]++ if count[word]

Not in Ruby you don't :-)

> count[word]=1 unless count[word]
>
> everytime! Is there a simple way to do it?

You could do:

   count = Hash.new(0)   # default to 0
   count[word] += 1      # each time through loop


David

-- 
                   David A. Black | dblack / wobblini.net
Author of "Ruby for Rails"   [1] | Ruby/Rails training & consultancy [3]
DABlog (DAB's Weblog)        [2] | Co-director, Ruby Central, Inc.   [4]
[1] http://www.manning.com/black | [3] http://www.rubypowerandlight.com
[2] http://dablog.rubypal.com    | [4] http://www.rubycentral.org