# Modifying Hash Default Value

**URL:** <https://www.ruby-forum.com/t/modifying-hash-default-value/59952>\
**Category:** Ruby\
**Created:** [May 8, 2006, 10:47pm UTC](https://www.ruby-forum.com/t/modifying-hash-default-value/59952 "2006-05-08T22:47:05Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![ljscoras](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/8c91f0/32.png) [@ljscoras](https://www.ruby-forum.com/u/ljscoras)\
**Post date:** [May 8, 2006, 10:47pm UTC](https://www.ruby-forum.com/t/modifying-hash-default-value/59952/1 "2006-05-08T22:47:05Z")

</div>

Don’t do this…This one bit me hard =)

```
hash = Hash.new( [] )
hash[:new_key] << 3

hash.length # => 0 ouch
hash.keys # => [] doh!

# this why this was painful...
hash[:new_key] # => [3]

hash[:another_key] # => [3]

```

So anyway, is there no way to have this do what I mean? or do I have  
to initialize it seperately?

```
hash[:new_key] ||= []
hash[:new_key] << 3

```

I’m having a total mental lapse, but I know using the block form won’t  
help either, i.e.:

```
hash = Hash.new { [] }

```

Any thoughts?

---

<div class="post-metadata">

**Author:** ![ljscoras](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/8c91f0/32.png) [@ljscoras](https://www.ruby-forum.com/u/ljscoras)\
**Post date:** [May 8, 2006, 10:50pm UTC](https://www.ruby-forum.com/t/modifying-hash-default-value/59952/2 "2006-05-08T22:50:29Z")

</div>

On May 8, 2006, at 3:44 PM, Louis J Scoras wrote:

> hash = Hash.new { [] }
> 
> Any thoughts?

Hash.new { |hash, key| hash[key] = Array.new }

😉

James Edward G. II

---

<div class="post-metadata">

**Author:** ![ljscoras](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/8c91f0/32.png) [@ljscoras](https://www.ruby-forum.com/u/ljscoras)\
**Post date:** [May 8, 2006, 11:05pm UTC](https://www.ruby-forum.com/t/modifying-hash-default-value/59952/3 "2006-05-08T23:05:41Z")

</div>

Heh, I knew it would end up being something obvious. Still, don’t do  
what I did =)

Thanks James!

---

<div class="post-metadata">

**Author:** ![ljscoras](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/8c91f0/32.png) [@ljscoras](https://www.ruby-forum.com/u/ljscoras)\
**Post date:** [May 9, 2006, 2:54pm UTC](https://www.ruby-forum.com/t/modifying-hash-default-value/59952/4 "2006-05-09T14:54:52Z")

</div>

On 5/9/06, Robert K. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> This comes up here roughly every three days. So you’re not alone if  
> that comforts you. 🙂

Heh. Nope, I’m almost never comforted by other people’s misfortunes,  
which is partly why I posted this.

It was actually a pretty basic (read stupid) error on my part, but my  
biggest blunder was actually /not reading the documentation/. As soon  
as I saw a block with two parameters associated with a hash, I  
immediately assumed that it was iteration with key/value pairs, which  
of course makes absolutely no sense in this context. Had I read the  
documentation for the hash constructor I would have seen that it  
passes the entire hash with the failed key, at which point the  
solution comes quite naturally as James pointed out above.

---

<div class="post-metadata">

**Author:** ![ljscoras](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/8c91f0/32.png) [@ljscoras](https://www.ruby-forum.com/u/ljscoras)\
**Post date:** [May 9, 2006, 9:41am UTC](https://www.ruby-forum.com/t/modifying-hash-default-value/59952/5 "2006-05-09T09:41:28Z")

</div>

2006/5/8, Louis J Scoras [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> Heh, I knew it would end up being something obvious. Still, don’t do  
> what I did =)

This comes up here roughly every three days. So you’re not alone if  
that comforts you. 🙂

robert
