A most undangerous Hash#store!


#1

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.


#2

On Mon, 8 Jan 2007, Trans wrote:

end
end

store_once ?

Kirk H.


#3

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


#4

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.


#5

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


#6

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


#7

On 2007-01-08 02:00:10 +0100, removed_email_address@domain.invalid 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”}


#8

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.


#9

Hi –

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

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

David


#10

Trans wrote:

hash *= {key=>value}

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

hash |= {key=>value}

T.


#11

Trans wrote:

  store(key,value)
  return value
end

end

T.

new_item


#12

removed_email_address@domain.invalid wrote:

store_once ?

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

T.


#13

Trans [mailto:removed_email_address@domain.invalid] :

# 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


#14

On 1/8/07, Trans removed_email_address@domain.invalid 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


#15

On 08.01.2007 02:59, removed_email_address@domain.invalid 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


#16

On 08.01.2007 02:00, removed_email_address@domain.invalid wrote:

You can use merge and a one-key hash:

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

Or even

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

robert


#17

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 # => {}


#18

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


#19

On 08.01.2007 13:37, removed_email_address@domain.invalid 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


#20

removed_email_address@domain.invalid schrieb:

 store(key,value)
 return value

end
end

store_once ?

how about store_unique ?