a most undangerous Hash#store!

T

Trans

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.
 
K

khaines

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

store_once ?


Kirk Haines
 
D

Daniel Schierbeck

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
 
D

Devin Mullins

Trans said:
# 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
 
T

Trans

Daniel said:
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.
 
D

dblack

Hi --

Daniel said:
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.

You can use merge and a one-key hash:

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


David

--
Q. What is THE Ruby book for Rails developers?
A. RUBY FOR RAILS by David A. Black (http://www.manning.com/black)
(See what readers are saying! http://www.rubypal.com/r4rrevs.pdf)
Q. Where can I get Ruby/Rails on-site training, consulting, coaching?
A. Ruby Power and Light, LLC (http://www.rubypal.com)
 
T

Trans

Devin said:
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.
 
D

dblack

Hi --

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"}

True -- that's a bit of a deal-breaker :)


David

--
Q. What is THE Ruby book for Rails developers?
A. RUBY FOR RAILS by David A. Black (http://www.manning.com/black)
(See what readers are saying! http://www.rubypal.com/r4rrevs.pdf)
Q. Where can I get Ruby/Rails on-site training, consulting, coaching?
A. Ruby Power and Light, LLC (http://www.rubypal.com)
 
W

William James

Trans said:
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
 
M

Martin DeMello

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
 
R

Robert Klemme

Hi --

Daniel said:
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.

You can use merge and a one-key hash:

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

Or even

hash.merge( :key => "new value" )

robert
 
R

Robert Klemme

Hi --

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"}

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
 
M

Mauricio Fernandez

On 2007-01-08 02:00:10 +0100, (e-mail address removed) 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"}

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.

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

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

dblack

Hi --

00:10 +0100, (e-mail address removed) 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"}

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.

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

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

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


David

--
Q. What is THE Ruby book for Rails developers?
A. RUBY FOR RAILS by David A. Black (http://www.manning.com/black)
(See what readers are saying! http://www.rubypal.com/r4rrevs.pdf)
Q. Where can I get Ruby/Rails on-site training, consulting, coaching?
A. Ruby Power and Light, LLC (http://www.rubypal.com)
 
M

Mauricio Fernandez

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

To be fair, the reference to #merge was not entirely misguided, since

a = {:bar => 1, :foo => 2}
{:foo => 0}.merge(a) # => {:bar=>1, :foo=>2}
{:baz => 0}.merge(a) # => {:baz=>0, :bar=>1, :foo=>2}

It just happens to work the other way around (cf. #reverse_merge mentioned
earlier in this thread).

Anyway, Trans was talking about destructive updates, so what about

def store?(key, value)
unless key?(key)
store(key,value)
return true
end
false
end

similar to the #store! he described, but with a more regular behavior when you
have nil/false values...
 
T

Trans

Mauricio said:
Anyway, Trans was talking about destructive updates, so what about

def store?(key, value)
unless key?(key)
store(key,value)
return true
end
false
end

similar to the #store! he described, but with a more regular behavior when you
have nil/false values...

well, i was thinking of combining Mr. Haines idea with Mr. James to
get #store_new, which seemed just about right. but now your suggestion
is quite interesting. at first i wasn't so sure, but thinking about it
a little more it makes sense -- "is something stored with this key? if
not store this value with it". i was also worried that it did not
return the stored value like the normal store, but i realize now that
could be ambigious if value=nil, so you may be on point. is that right?
if so then i have to go with your suggestion.

t.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,222
Messages
2,571,140
Members
47,755
Latest member
Grazynkaa

Latest Threads

Top