Trans
January 7, 2007, 11:43pm
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.
Trans
January 8, 2007, 1:02am
2
On Mon, 8 Jan 2007, Trans wrote:
end
end
store_once ?
Kirk H.
Trans
January 8, 2007, 1:20am
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
Trans
January 8, 2007, 1:48am
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.
Trans
January 8, 2007, 2:00am
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
Trans
January 8, 2007, 1:23am
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
Trans
January 8, 2007, 2:15am
7
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”}
Trans
January 8, 2007, 2:49am
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.
Trans
January 8, 2007, 2:59am
9
Hi –
On Mon, 8 Jan 2007, Florian G. wrote:
value"}
True – that’s a bit of a deal-breaker
David
Trans
January 8, 2007, 3:00am
10
Trans wrote:
hash *= {key=>value}
Hmm… it just occured to me that maybe this would be better defined
as:
hash |= {key=>value}
T.
Trans
January 8, 2007, 3:04am
12
[email protected] wrote:
store_once ?
That’s pretty good. If nothing better come up I’ll use that. Thanks.
T.
Trans
January 8, 2007, 5:13am
13
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
Trans
January 8, 2007, 6:34am
14
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
Trans
January 8, 2007, 12:21pm
15
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
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
Trans
January 8, 2007, 12:15pm
16
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
Trans
January 8, 2007, 1:25pm
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 # => {}
Trans
January 8, 2007, 1:37pm
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
Trans
January 8, 2007, 3:00pm
19
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
Trans
January 8, 2007, 2:41pm
20
[email protected] schrieb:
store(key,value)
return value
end
end
store_once ?
how about store_unique ?