###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 prefer the first because
each_with_object
is like a map to me, easy to identify with and read. My hang up is with what's in the block.How about...
I'm sure there's an iteration or two there but anytime I have to read and "process" something mentally, I always wonder if I can give it a name and abstract it.