# What does this hash do? Is this a hash?

**URL:** <https://www.ruby-forum.com/t/what-does-this-hash-do-is-this-a-hash/56722>\
**Category:** Ruby\
**Created:** [April 3, 2006, 11:31am UTC](https://www.ruby-forum.com/t/what-does-this-hash-do-is-this-a-hash/56722 "2006-04-03T11:31:44Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![ralf](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/c89c15/32.png) [@ralf](https://www.ruby-forum.com/u/ralf)\
**Post date:** [April 3, 2006, 11:31am UTC](https://www.ruby-forum.com/t/what-does-this-hash-do-is-this-a-hash/56722/1 "2006-04-03T11:31:44Z")

</div>

Hi,  
I’m tryin some hash functionality and came accros this in irb:

hash = Hash.new("")  
=\> {}  
hash[“a”] \<\< “b”  
=\> “b”  
hash[“a”]  
=\> “b”  
hash  
=\> {}  
hash.size  
=\> 0  
hash[“alskj”]  
=\> “b”

What ist this? Prog.Ruby says, that by “hash = Hash.new(”")" hash has a  
default value for missing keys.  
How can I set a value for a keys with the \<\<-method?

Thanks in advance  
Ralf

---

<div class="post-metadata">

**Author:** ![ralf](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/c89c15/32.png) [@ralf](https://www.ruby-forum.com/u/ralf)\
**Post date:** [April 3, 2006, 11:37am UTC](https://www.ruby-forum.com/t/what-does-this-hash-do-is-this-a-hash/56722/2 "2006-04-03T11:37:52Z")

</div>

> > > > > “r” == ralf [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) writes:

r\> hash = Hash.new(“”)  
r\> =\> {}  
r\> hash[“a”] \<\< “b”

When you write this :

1. ruby return the default value for the hash (‘a’ don’t exist as a  
key)
2. concatenate “b” to this default value (see String#\<\<)

Finally you have modified the default value for the hash, and you can  
see  
this here :

r\> hash[“alskj”]  
r\> =\> “b”

ruby now give the new default value

Guy Decoux

---

<div class="post-metadata">

**Author:** ![ralf](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/c89c15/32.png) [@ralf](https://www.ruby-forum.com/u/ralf)\
**Post date:** [April 3, 2006, 11:50am UTC](https://www.ruby-forum.com/t/what-does-this-hash-do-is-this-a-hash/56722/3 "2006-04-03T11:50:19Z")

</div>

Thanks!  
I think, it’s a bit confusing, that the dafault value is accessed by  
“hash[“a”]”, i.e. the term for accessing a value according to key “a”.

The constuctor “Hash.new {block}” works for me:  
ahash = Hash.new {|hash,key| hash[key] = “”}  
=\> {}  
ahash[“a”] \<\< “vier”  
=\> “vier”  
ahash  
=\> {“a”=\>“vier”}

Best regards  
Ralf

---

<div class="post-metadata">

**Author:** ![ralf](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/c89c15/32.png) [@ralf](https://www.ruby-forum.com/u/ralf)\
**Post date:** [April 3, 2006, 1:49pm UTC](https://www.ruby-forum.com/t/what-does-this-hash-do-is-this-a-hash/56722/4 "2006-04-03T13:49:01Z")

</div>

On Mon, Apr 03, 2006 at 06:34:50PM +0900, ts wrote:  
} \>\>\>\>\> “r” == ralf [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) writes:  
}  
} r\> hash = Hash.new(“”)  
} r\> =\> {}  
} r\> hash[“a”] \<\< “b”  
}  
} When you write this :  
} 1) ruby return the default value for the hash (‘a’ don’t exist as a  
key)  
} 2) concatenate “b” to this default value (see String#\<\<)  
}  
} Finally you have modified the default value for the hash, and you can  
see  
} this here :  
}  
} r\> hash[“alskj”]  
} r\> =\> “b”  
}  
} ruby now give the new default value

This has to be the most frequently asked question on this list. Why  
isn’t it  
in the FAQ at [http://www.rubygarden.org/faq/main/all](http://www.rubygarden.org/faq/main/all) ?

} Guy Decoux  
–Greg
