From: Mayank Kohaley Date: 2011-12-09T16:06:40+09:00 Subject: Re: How can I do h["foo"] += "bar" if h["foo"] does not exist? --20cf305e25a502541504b3a36d5e Content-Type: text/plain; charset=UTF-8 On Fri, Dec 9, 2011 at 11:28 AM, Andrew S. 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"] --20cf305e25a502541504b3a36d5e--