# How to re-replace Hash default value behaviour?

**URL:** <https://www.ruby-forum.com/t/how-to-re-replace-hash-default-value-behaviour/137119>\
**Category:** Ruby\
**Created:** [May 9, 2008, 2:47am UTC](https://www.ruby-forum.com/t/how-to-re-replace-hash-default-value-behaviour/137119 "2008-05-09T02:47:15Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![steenslag](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/c57346/32.png) [@steenslag](https://www.ruby-forum.com/u/steenslag)\
**Post date:** [May 9, 2008, 2:47am UTC](https://www.ruby-forum.com/t/how-to-re-replace-hash-default-value-behaviour/137119/1 "2008-05-09T02:47:15Z")

</div>

Hash.new(0) and the block form of Hash.new{_smart\_stuff_} come in  
really handy while reading in a hash.  
When the reading is done however, my hash serves to provide data. At  
that stage, the default\_value behaviour gets in the way. For instance,  
if the hash contains the number of goals for soccerplayers, I don’t want  
the hash to return value “0” for the key “Wolfgang Amadeus Mozart”; by  
then I want plain old Nil, or “No data available”.

failed experiment:

Class Hash  
def default\_proc  
Nil  
end  
end

How can Hash.new behaviour be changed?

regards,  
Siep

---

<div class="post-metadata">

**Author:** ![steenslag](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/c57346/32.png) [@steenslag](https://www.ruby-forum.com/u/steenslag)\
**Post date:** [May 9, 2008, 4:25am UTC](https://www.ruby-forum.com/t/how-to-re-replace-hash-default-value-behaviour/137119/2 "2008-05-09T04:25:52Z")

</div>

Siep K. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Class Hash  
> def default\_proc  
> Nil  
> end  
> end
> 
> How can Hash.new behaviour be changed?
> 
> regards,  
> Siep

h = Hash.new.merge(h)

So for example:

h = Hash.new(0)  
h[:a] += 2  
h[:a] += 1  
h[:b] += 4  
p h #=\> {:a=\>3, :b=\>4}

h = Hash.new.merge(h)  
p h[:c] #=\> nil, it’s now returning plain old nil

m.

---

<div class="post-metadata">

**Author:** ![steenslag](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/c57346/32.png) [@steenslag](https://www.ruby-forum.com/u/steenslag)\
**Post date:** [May 9, 2008, 4:28am UTC](https://www.ruby-forum.com/t/how-to-re-replace-hash-default-value-behaviour/137119/3 "2008-05-09T04:28:12Z")

</div>

Reset the default.

> > a = Hash.new(0)  
> > =\> {}
> 
> > a[“joe”]  
> > =\> 0
> 
> > a.default = nil  
> > =\> nil
> 
> > a[“joe”]  
> > =\> nil

Regards,  
Craig

---

<div class="post-metadata">

**Author:** ![steenslag](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/c57346/32.png) [@steenslag](https://www.ruby-forum.com/u/steenslag)\
**Post date:** [May 9, 2008, 11:09am UTC](https://www.ruby-forum.com/t/how-to-re-replace-hash-default-value-behaviour/137119/4 "2008-05-09T11:09:29Z")

</div>

Siep K. wrote:

> Hash.new(0) and the block form of Hash.new{_smart\_stuff_} come in  
> really handy while reading in a hash.  
> When the reading is done however, my hash serves to provide data. At  
> that stage, the default\_value behaviour gets in the way. For instance,  
> if the hash contains the number of goals for soccerplayers, I don’t want  
> the hash to return value “0” for the key “Wolfgang Amadeus Mozart”; by  
> then I want plain old Nil, or “No data available”.
> 
> failed experiment:
> 
> Class Hash  
> def default\_proc  
> Nil  
> end  
> end
> 
> How can Hash.new behaviour be changed?
> 
> regards,  
> Siep

a = Hash.new(0)

# do something with a

a = Hash.new { |h,k| h[k] = “No data available” }.merge(a)

But I don’t know if this is what you want.

Bye.  
Andrea

---

<div class="post-metadata">

**Author:** ![steenslag](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/c57346/32.png) [@steenslag](https://www.ruby-forum.com/u/steenslag)\
**Post date:** [May 10, 2008, 1:42am UTC](https://www.ruby-forum.com/t/how-to-re-replace-hash-default-value-behaviour/137119/5 "2008-05-10T01:42:14Z")

</div>

Craig D. wrote:

> Reset the default.
> 
> > > a = Hash.new(0)  
> > > =\> {}
> > 
> > > a[“joe”]  
> > > =\> 0
> > 
> > > a.default = nil  
> > > =\> nil
> > 
> > > a[“joe”]  
> > > =\> nil
> 
> Regards,  
> Craig

Thanks all. When i’m stuck, the solution is almost always easier then I  
imagined.

Regards,

Siep
