Object initialization problem

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]
count[word]=1 unless count[word]

everytime! Is there a simple way to do it?

Thanks :slight_smile:
Vimal

Hi --

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 :slight_smile:

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

路路路

On Mon, 25 Sep 2006, Vimal wrote:

--
                   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] Ruby for Rails | [3] http://www.rubypowerandlight.com
[2] http://dablog.rubypal.com | [4] http://www.rubycentral.org

class Hash
  def increment_word_count(key)
     self[key] ||= 1
     self[key] += 1
  end
end

count = Hash.new
count.increment_word_count(word)

Of course, if you can think of a better method name, but this will help with
DRYness.

Jason

路路路

On 9/25/06, Vimal <j.vimal@gmail.com> 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]
count[word]=1 unless count[word]

everytime! Is there a simple way to do it?

Thanks :slight_smile:
Vimal

Hi --

路路路

On Mon, 25 Sep 2006, Jason Roelofs wrote:

class Hash
def increment_word_count(key)
   self[key] ||= 1
   self[key] += 1
end

count = Hash.new
count.increment_word_count(word)

Of course, if you can think of a better method name, but this will help with
DRYness.

There's also DRR (Don't Repeat Ruby :slight_smile: Hashes are already good at
this stuff (see my previous response).

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] Ruby for Rails | [3] http://www.rubypowerandlight.com
[2] http://dablog.rubypal.com | [4] http://www.rubycentral.org

Doh! I'm getting caught up in the cooler features of Ruby and forgetting the
simple stuff. David's got it right.

Jason

路路路

On 9/25/06, dblack@wobblini.net <dblack@wobblini.net> wrote:

Hi --

On Mon, 25 Sep 2006, Jason Roelofs wrote:

> class Hash
> def increment_word_count(key)
> self[key] ||= 1
> self[key] += 1
> end
>
> count = Hash.new
> count.increment_word_count(word)
>
> Of course, if you can think of a better method name, but this will help
with
> DRYness.

There's also DRR (Don't Repeat Ruby :slight_smile: Hashes are already good at
this stuff (see my previous response).

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] Ruby for Rails | [3] http://www.rubypowerandlight.com
[2] http://dablog.rubypal.com | [4] http://www.rubycentral.org