[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Hash.new(obj) of any signifficant value?

Trans

7/15/2006 1:03:00 AM

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.


3 Answers

Ryan Davis

7/15/2006 1:25:00 AM

0


On Jul 14, 2006, at 6:02 PM, transfire@gmail.com 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...


Trans

7/15/2006 3:15:00 AM

0


Ryan Davis wrote:
> On Jul 14, 2006, at 6:02 PM, transfire@gmail.com 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.


Morton Goldberg

7/15/2006 5:52:00 AM

0

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

On Jul 14, 2006, at 9:02 PM, transfire@gmail.com 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_.