# Hash.new(obj) of any signifficant value?

**URL:** <https://www.ruby-forum.com/t/hash-new-obj-of-any-signifficant-value/98500>\
**Category:** Ruby\
**Created:** [July 15, 2006, 3:03am UTC](https://www.ruby-forum.com/t/hash-new-obj-of-any-signifficant-value/98500 "2006-07-15T03:03:36Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [July 15, 2006, 3:03am UTC](https://www.ruby-forum.com/t/hash-new-obj-of-any-signifficant-value/98500/1 "2006-07-15T03:03:36Z")

</div>

Is this form of Hash initialization really very useful?

```
 Hash.new(obj) => aHash

  If _obj_ is specified, this single object will be used for all

```

_default values_.

Since the object is the _same_ object every time, I have never found a  
use for it. I would much rather:

```
 Hash.new( hash ) => aHash

  If _hash_ is specified, the key-value pairs of this hash will

```

populate the new hash.

Besides, you can still get the former behavior like this:

```
 Hash.new { |h,k| h[k] = obj }

```

And if that’s not enough then:

Hash.new\_with\_default(obj)

T.

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [July 15, 2006, 3:28am UTC](https://www.ruby-forum.com/t/hash-new-obj-of-any-signifficant-value/98500/2 "2006-07-15T03:28:22Z")

</div>

On Jul 14, 2006, at 6:02 PM, removed\_email\_address@domain.invalid wrote:

> Is this form of Hash initialization really very useful?
> 
> ```
> Hash.new(obj) => aHash
> 
> If _obj_ is specified, this single object will be used for all
> 
> ```
> 
> _default values_.

I use Hash.new(0) constantly…

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [July 15, 2006, 5:15am UTC](https://www.ruby-forum.com/t/hash-new-obj-of-any-signifficant-value/98500/3 "2006-07-15T05:15:51Z")

</div>

Ryan D. wrote:

> On Jul 14, 2006, at 6:02 PM, removed\_email\_address@domain.invalid wrote:
> 
> > Is this form of Hash initialization really very useful?
> > 
> > ```
> > Hash.new(obj) => aHash
> > 
> > If _obj_ is specified, this single object will be used for all
> > 
> > ```
> > 
> > _default values_.
> 
> I use Hash.new(0) constantly…

Okay, I can see that.

T.

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [July 15, 2006, 7:52am UTC](https://www.ruby-forum.com/t/hash-new-obj-of-any-signifficant-value/98500/4 "2006-07-15T07:52:43Z")

</div>

I found the following useful

```
include Curses

DISPATCH = Hash.new(:beep)
DISPATCH[?A] = :abort # abort
DISPATCH[?h] = :key_help # show help
# ...
DISPATCH[?z] = :undo # undo previous move

```

for implementing a keystroke command dispatch table.

Regards, Morton
