Hash.new(obj) of any signifficant value?

T

transfire

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.
 
R

Ryan Davis

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...
 
M

Morton Goldberg

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,209
Messages
2,571,085
Members
47,683
Latest member
AustinFairchild

Latest Threads

Top