Topic: hash merge and update

I basically need to add an key and value pair to an hash based on condition.
The following code works.
hash = { :key1 => value1, :key2 => value2 }

result = expr > value ? 100: nil

if (!result.nil?)

   hash.update({ :key3 => value3

How can i do that succintly?

what is the difference between merge and update?

Re: hash merge and update

You can do this:

hash[:key] = 100 if exp > value

Merge and update are very similar, the only difference is "merge" returns a new hash result, update changes the hash in place. So "merge!" with the exclemation mark is the same as "update".

Railscasts - Free Ruby on Rails Screencasts