I have a Hash in Ruby:
hash = Hash.new It has some key value pairs in it, say:
hash[1] = "One" hash[2] = "Two" If the hash contains a key 2, then I want to add "Bananas" to its value. If the hash doesn't have a key 2, I want to create a new key value pair 2=>"Bananas".
I know I can do this by first checkng whether the hash has the key 2 by using has_key? and then act accordingly. But this requires an if statement and more than one line.
So is there a simple, elegant one-liner for achieving this?