a most undangerous Hash#store!

M

Mauricio Fernandez

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?

Yes, that's the idea ("more regular behavior"), but as Robert said, a
predicate method with side-effects seems _very_ dangerous (yes, store!? would
be more accurate :).

So it might make sense to name it e.g. #store_once, with the above
semantics. Returning the value that was passed to the method makes little
sense: it doesn't provide any information we didn't have to being with.
 
T

Trans

Mauricio said:
Yes, that's the idea ("more regular behavior"), but as Robert said, a
predicate method with side-effects seems _very_ dangerous (yes, store!? would
be more accurate :).

So it might make sense to name it e.g. #store_once, with the above
semantics. Returning the value that was passed to the method makes little
sense: it doesn't provide any information we didn't have to being with.

i agree. i will use that semantic but with a different name. after
further consideration I thought of:

insert(key,value)

i think its apt. argree?

t.
 
M

Mauricio Fernandez

i agree. i will use that semantic but with a different name. after
further consideration I thought of:

insert(key,value)

i think its apt. argree?

The problem with that name is that there's no indication of the "just once"
semantics, is there? Instead, it makes one think of String#insert and
Array#insert, and wonder what #insert translates to in an unordered container
(for instance, one interpretation would be that the value will be
pushed/unshifted to the array associated to the key, assuming that the hash
holds arrays/sets).
So it adds to the list of things we have to memorize.

Do you feel that store_{once,new,unique} is too long?
(I have a slight preference for "once"; "unique" seems a bit puzzling since it
seems to refer to the key, which is always unique)
 
M

Martin DeMello

Do you feel that store_{once,new,unique} is too long?
(I have a slight preference for "once"; "unique" seems a bit puzzling since it
seems to refer to the key, which is always unique)

Even 'store_once' seems to refer to the value rather than the pair,
which is why I like "underlay" - it suggests that if there is
something already in the hash, it will mask the value you are trying
to add.

martin
 
W

Wilson Bilkovich

Can we please just agree to write:
hash[key] = value unless hash.key?(key)
I mean.. how often do you do this?
 
T

Trans

it's okay. but i would prefer the best and most concise name reasonably
possible, of course.
Even 'store_once' seems to refer to the value rather than the pair,
which is why I like "underlay" - it suggests that if there is
something already in the hash, it will mask the value you are trying
to add.

ah, 'underlay' as opposed to 'overwrite'. i see where you're coming
from there. with #insert i was thinking about databases where insert is
used to add a record but not to modify it -- ie. if the record key is
already present you can't insert a new one.

i understand that 'insert' isn't prefect though. i tend to prefer
keeping array and hash in sync for polymorphism's sake, but #insert
just doesn't have an array-like meaning since it isn't ordered. but
maybe an even simpler tern would suffice -- #add.

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

No members online now.

Forum statistics

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

Latest Threads

Top