Andrew S. wrote in post #1035841:
> Hi there,
>
> I have a hash with various keys and values that are strings.
>
> If the key exists, the following works: h["foo"] += "bar"
>
> But if the key does not exist, I get an error.
>
> Is it possible _in one statement_ to cover both the cases where the key
> does exist (as above) AND the case where the key does not exist - and in
> this situation I'd like the new key and value pair to be created in the
> hash.
>
> Any help gratefully received!
>
>  - A

see if this solves your problem:
=begin
One liner to update an hash of string values:
h[:key] = h.has_key?(:key) ? h[:key] += '-New_value' : 'First_value'
=end
# <test>
h = {}
h[:foo] = h.has_key?(:foo) ? h[:foo] += '-foo_2nd' : 'foo_1st'
h[:bar] = h.has_key?(:bar) ? h[:bar] += '-bar_2nd' : 'bar_1st'
p 'From empty hash: ' + h.inspect
h[:foo] = h.has_key?(:foo) ? h[:foo] += '-foo_2nd' : 'foo_1st'
h[:bar] = h.has_key?(:bar) ? h[:bar] += '-bar_2nd' : 'bar_1st'
p 'Updated hash...: ' +  h.inspect
exit(0)
# </test>
=begin
Output:
"From empty hash: {:foo=>\"foo_1st\"- :bar=>\"bar_1st\"}"
"Updated hash...: {:foo=>\"foo_1st-foo_2nd\"-
:bar=>\"bar_1st-bar_2nd\"}"
=end
HTH gfb

-- 
Posted via http://www.ruby-forum.com/.