###Given
DEFAULTS = {
foo: lambda { ENV['FOO'] },
bar: lambda { ENV['BAR'] },
bazz: 0.1
}
##Which is preferred:
defaults = DEFAULTS.each_with_object({}) do |(k, v), hash|
hash[k] = v.respond_to?(:call) ? v.call : v
end
OR
defaults = DEFAULTS.dup
defaults.each do |k, v|
defaults[k] = v.call if v.respond_to?(:call)
end
I like the idea of extracting out the logic into a new method, especially since it makes a nice one liner, and we know how I feel about one line methods.