Hash.new(obj) of any signifficant value?

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.

On Jul 14, 2006, at 6:02 PM, [email protected] 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…

Ryan D. wrote:

On Jul 14, 2006, at 6:02 PM, [email protected] 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.

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

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs