[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

a most undangerous Hash#store!

Trans

1/7/2007 10:42:00 PM

Hi--

Think I need a better name for this method. It is anything but
dangerous. Any ideas?

# As with #store but adds the key/value pair
# only if the key isn't already in the hash.

def store!(key, value)
unless key?(key)
store(key,value)
return value
end
end

T.


29 Answers

khaines

1/8/2007 12:01:00 AM

0

Daniel Schierbeck

1/8/2007 12:20:00 AM

0

On Mon, 2007-01-08 at 07:42 +0900, Trans wrote:
> Hi--
>
> Think I need a better name for this method. It is anything but
> dangerous. Any ideas?
>
> # As with #store but adds the key/value pair
> # only if the key isn't already in the hash.
>
> def store!(key, value)
> unless key?(key)
> store(key,value)
> return value
> end
> end

Is there really a need for such a method? Why not simply:

hsh[:key] ||= "value"

or, if you have a default value on your hash:

hsh[:key] = "value" unless hsh.has_key? :key

I'm positive you've thought about this, but I cannot find a reason why
such a method should be necessary.


Cheers,
Daniel


Devin Mullins

1/8/2007 12:23:00 AM

0

Trans wrote:
> # As with #store but adds the key/value pair
> # only if the key isn't already in the hash.
Hrm. ActiveSupport has something similar. You can do
hash.reverse_merge(key => value).

HNTH,
Devin

Trans

1/8/2007 12:48:00 AM

0


Daniel Schierbeck wrote:

> Is there really a need for such a method? Why not simply:
>
> hsh[:key] ||= "value"

This isn't quite the same becasue it looks to see if tha value is nil
or false, not if the key is there or not.

> or, if you have a default value on your hash:
>
> hsh[:key] = "value" unless hsh.has_key? :key

Yes, certianly. nad that woul dbe fine if I were just needing here and
ther, but I have need for using it quite often.

> I'm positive you've thought about this, but I cannot find a reason why
> such a method should be necessary.

HTH,
T.


dblack

1/8/2007 1:00:00 AM

0

Trans

1/8/2007 1:49:00 AM

0


Devin Mullins wrote:
> Trans wrote:
> > # As with #store but adds the key/value pair
> > # only if the key isn't already in the hash.
> Hrm. ActiveSupport has something similar. You can do
> hash.reverse_merge(key => value).

Astute! Indeed, I am using that too. Though I defined an operator for
it instead:

hash *= {key=>value}

(I use + as an alias for regular merge, btw.) In general though I would
prefer a simple conditional store method b/c it's (probably) more
efficient for a small numbers of entries and it also reads better.

T.


dblack

1/8/2007 1:59:00 AM

0

Trans

1/8/2007 2:00:00 AM

0


Trans wrote:
> hash *= {key=>value}

Hmm... it just occured to me that maybe this would be better defined
as:

hash |= {key=>value}

T.


Trans

1/8/2007 2:04:00 AM

0


khaines@enigo.com wrote:

> store_once ?

That's pretty good. If nothing better come up I'll use that. Thanks.

T.


William James

1/8/2007 3:46:00 AM

0


Trans wrote:
> Hi--
>
> Think I need a better name for this method. It is anything but
> dangerous. Any ideas?
>
> # As with #store but adds the key/value pair
> # only if the key isn't already in the hash.
>
> def store!(key, value)
> unless key?(key)
> store(key,value)
> return value
> end
> end
>
> T.

new_item