On Fri, Dec 9, 2011 at 11:28 AM, Andrew S. <andrewinfosec / gmail.com> wrote:

> 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
>
> --
> Posted via http://www.ruby-forum.com/.
>
>

You can have something like this to check whether the key is present or
not-

h["key"] += value if h["key"]