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
Same idea as Chris Jester-Young, with a slight readability trick
def cond(x) condition ? x : :delete_me end hash = { :key1 => value1, :key2 => cond(value2), :key3 => value3 }
and then postprocess to remove the :delete_me entries
:delete_me