# Object initialization problem

**URL:** <https://www.ruby-forum.com/t/object-initialization-problem/69918>\
**Category:** Ruby\
**Created:** [September 25, 2006, 4:24pm UTC](https://www.ruby-forum.com/t/object-initialization-problem/69918 "2006-09-25T16:24:39Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Vimal](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/d2c977/32.png) [@Vimal](https://www.ruby-forum.com/u/Vimal)\
**Post date:** [September 25, 2006, 4:24pm UTC](https://www.ruby-forum.com/t/object-initialization-problem/69918/1 "2006-09-25T16:24:39Z")

</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:** ![Vimal](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/d2c977/32.png) [@Vimal](https://www.ruby-forum.com/u/Vimal)\
**Post date:** [September 25, 2006, 4:28pm UTC](https://www.ruby-forum.com/t/object-initialization-problem/69918/2 "2006-09-25T16:28:40Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Vimal](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/d2c977/32.png) [@Vimal](https://www.ruby-forum.com/u/Vimal)\
**Post date:** [September 25, 2006, 4:28pm UTC](https://www.ruby-forum.com/t/object-initialization-problem/69918/3 "2006-09-25T16:28:40Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Vimal](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/d2c977/32.png) [@Vimal](https://www.ruby-forum.com/u/Vimal)\
**Post date:** [September 25, 2006, 4:42pm UTC](https://www.ruby-forum.com/t/object-initialization-problem/69918/4 "2006-09-25T16:42:44Z")

</div>

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

Jason

---

<div class="post-metadata">

**Author:** ![Vimal](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/d2c977/32.png) [@Vimal](https://www.ruby-forum.com/u/Vimal)\
**Post date:** [September 25, 2006, 4:34pm UTC](https://www.ruby-forum.com/t/object-initialization-problem/69918/5 "2006-09-25T16:34:42Z")

</div>

Hi –

On Mon, 25 Sep 2006, Jason R. wrote:

> 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
