Building a hash in a conditional way

I am using Ruby on Rails 3.0.10 and I would like to build an hash key\value pairs in a conditional way. That is, I would like to add a key and its related value if a condition is matched:

hash = {
  :key1 => value1,
  :key2 => value2, # This key2\value2 pair should be added only 'if condition' is 'true'
  :key3 => value3,
  ...
}

How can I do that and keep a "good" readability for the code? Am I "forced" to use the merge method?


Solution 1:

I prefer tap, as I think it provides a cleaner solution than the ones described here by not requiring any hacky deleting of elements and by clearly defining the scope in which the hash is being built.

It also means you don't need to declare an unnecessary local variable, which I always hate.

In case you haven't come across it before, tap is very simple - it's a method on Object that accepts a block and always returns the object it was called on. So to build up a hash conditionally you could do this:

Hash.new.tap do |my_hash|
  my_hash[:x] = 1 if condition_1
  my_hash[:y] = 2 if condition_2
  ...
end

There are many interesting uses for tap, this is just one.

Solution 2:

A functional approach with Hash.compact:

hash = {
  :key1 => 1,
  :key2 => (2 if condition),
  :key3 => 3,
}.compact 

Solution 3:

Probably best to keep it simple if you're concerned about readability:

hash = {}
hash[:key1] = value1
hash[:key2] = value2 if condition?
hash[:key3] = value3
...

Solution 4:

Keep it simple:

hash = {
  key1: value1,
  key3: value3,
}

hash[:key2] = value2 if condition

This way you also visually separate your special case, which might get unnoticed if it is buried within hash literal assignment.

Solution 5:

I use merge and the ternary operator for that situation,

hash = {
  :key1 => value1,
  :key3 => value3,
  ...
}.merge(condition ? {:key2 => value2} : {})