A most undangerous Hash#store!

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.

On Mon, 8 Jan 2007, Trans wrote:

end
end

store_once ?

Kirk H.

On Mon, 2007-01-08 at 07:42 +0900, Trans wrote:

  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

Daniel S. 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.

Hi –

On Mon, 8 Jan 2007, Trans wrote:

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.

You can use merge and a one-key hash:

hash.merge({ :key => “new value” })

David

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

On 2007-01-08 02:00:10 +0100, [email protected] said:

hsh[:key] = “value” unless hsh.has_key? :key
You can use merge and a one-key hash:

hash.merge({ :key => “new value” })

Hm, not really:

{ :key => “old value” }.merge({ :key => “new value” }) # => {:key=>“new
value”}

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

Hi –

On Mon, 8 Jan 2007, Florian G. wrote:

value"}
True – that’s a bit of a deal-breaker :slight_smile:

David

Trans wrote:

hash *= {key=>value}

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

hash |= {key=>value}

T.

Trans wrote:

  store(key,value)
  return value
end

end

T.

new_item

[email protected] wrote:

store_once ?

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

T.

Trans [mailto:[email protected]] :

# As with #store but adds the key/value pair

# only if the key isn’t already in the hash.

just a suggestion: *not another new method name (since we’re still
updating/merging, right?). just add an option --if possible ie

merge :nodup
update :nodup

merge require => :nodup
update require => :nodup

merge :safe
update :safe

merge require => :safe
update require => :safe

On 1/8/07, Trans [email protected] 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.

#underlay

martin

On 08.01.2007 02:59, [email protected] wrote:

Hm, not really:

{ :key => “old value” }.merge({ :key => “new value” }) # =>
{:key=>“new value”}

True – that’s a bit of a deal-breaker :slight_smile:

But only if you need the return value:

irb(main):001:0> hash={:key=>1}
=> {:key=>1}
irb(main):002:0> hash.merge( :key => “new value” )
=> {:key=>“new value”}
irb(main):003:0> hash
=> {:key=>1}

The hash is merged properly.

robert

On 08.01.2007 02:00, [email protected] wrote:

You can use merge and a one-key hash:

hash.merge({ :key => “new value” })

Or even

hash.merge( :key => “new value” )

robert

On Mon, Jan 08, 2007 at 08:20:05PM +0900, Robert K. wrote:

=> {:key=>“new value”}
irb(main):003:0> hash
=> {:key=>1}

The hash is merged properly.

The original hash is not modified by #merge.
#merge != #merge! (== #update)

a = {}
a.merge(:foo => 1) # => {:foo=>1}
a # => {}

Hi –

On Mon, 8 Jan 2007, Mauricio F. wrote:

Hm, not really:
irb(main):002:0> hash.merge( :key => “new value” )
a.merge(:foo => 1) # => {:foo=>1}
a # => {}

Yes, merge was just a wrong turn on my part. Forget it.

David

On 08.01.2007 13:37, [email protected] wrote:

Yes, merge was just a wrong turn on my part. Forget it.

Done. And sorry for the noise (my noise that is).

Kind regards

robert

[email protected] schrieb:

 store(key,value)
 return value

end
end

store_once ?

how about store_unique ?