# Object initialization problem

**URL:** <https://rubytalk.org/t/object-initialization-problem/31307>\
**Category:** ruby-talk\
**Created:** [25 September 2006 14:23 UTC](https://rubytalk.org/t/object-initialization-problem/31307 "2006-09-25T14:23:26Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Vimal1](https://avatars.discourse-cdn.com/v4/letter/v/a5b964/32.png) [@Vimal1](https://rubytalk.org/u/Vimal1)\
**Post date:** [25 September 2006 14:23 UTC](https://rubytalk.org/t/object-initialization-problem/31307/1 "2006-09-25T14:23:26Z")

</div>

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 🙂  
Vimal

---

<div class="post-metadata">

**Author:** ![David\_A\_Black3](https://avatars.discourse-cdn.com/v4/letter/d/6a8cbe/32.png) [@David\_A\_Black3](https://rubytalk.org/u/David_A_Black3)\
**Post date:** [25 September 2006 14:27 UTC](https://rubytalk.org/t/object-initialization-problem/31307/2 "2006-09-25T14:27:35Z")

</div>

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 🙂

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

You could do:

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

David

> **···**
>
> On Mon, 25 Sep 2006, Vimal wrote:
> 
> --  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;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](http://www.manning.com/black) | [3] [http://www.rubypowerandlight.com](http://www.rubypowerandlight.com)  
> [2] [http://dablog.rubypal.com](http://dablog.rubypal.com) | [4] [http://www.rubycentral.org](http://www.rubycentral.org)

---

<div class="post-metadata">

**Author:** ![Jason\_R](https://avatars.discourse-cdn.com/v4/letter/j/94ad74/32.png) [@Jason\_R](https://rubytalk.org/u/Jason_R)\
**Post date:** [25 September 2006 14:28 UTC](https://rubytalk.org/t/object-initialization-problem/31307/3 "2006-09-25T14:28:09Z")

</div>

class Hash  
&nbsp;&nbsp;def increment\_word\_count(key)  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;self[key] ||= 1  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;self[key] += 1  
&nbsp;&nbsp;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 🙂  
> > Vimal

---

<div class="post-metadata">

**Author:** ![David\_A\_Black3](https://avatars.discourse-cdn.com/v4/letter/d/6a8cbe/32.png) [@David\_A\_Black3](https://rubytalk.org/u/David_A_Black3)\
**Post date:** [25 September 2006 14:33 UTC](https://rubytalk.org/t/object-initialization-problem/31307/4 "2006-09-25T14:33:16Z")

</div>

Hi --

> **···**
>
> On Mon, 25 Sep 2006, Jason Roelofs wrote:
> 
> > class Hash  
> > def increment\_word\_count(key)  
> > &nbsp;&nbsp;&nbsp;self[key] ||= 1  
> > &nbsp;&nbsp;&nbsp;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 🙂 Hashes are already good at  
> this stuff (see my previous response).
> 
> David
> 
> --  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;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](http://www.manning.com/black) | [3] [http://www.rubypowerandlight.com](http://www.rubypowerandlight.com)  
> [2] [http://dablog.rubypal.com](http://dablog.rubypal.com) | [4] [http://www.rubycentral.org](http://www.rubycentral.org)

---

<div class="post-metadata">

**Author:** ![Jason\_R](https://avatars.discourse-cdn.com/v4/letter/j/94ad74/32.png) [@Jason\_R](https://rubytalk.org/u/Jason_R)\
**Post date:** [25 September 2006 14:40 UTC](https://rubytalk.org/t/object-initialization-problem/31307/5 "2006-09-25T14:40:26Z")

</div>

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 🙂 Hashes are already good at  
> > this stuff (see my previous response).
> > 
> > David
> > 
> > --  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;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](http://www.manning.com/black) | [3] [http://www.rubypowerandlight.com](http://www.rubypowerandlight.com)  
> > [2] [http://dablog.rubypal.com](http://dablog.rubypal.com) | [4] [http://www.rubycentral.org](http://www.rubycentral.org)
