Which version of this Ruby code would you prefer to see in your codebase?
def properties_names_hash
out = {}
properties.each do |property|
out[property.name] = property.original_name
end
out
end
or
def properties_names_hash
properties.each_with_object({}) do |property, memo|
memo[property.name] = property.original_name
end
end
Coming from Java to Ruby, it was always more natural to me to think about the code in terms of the first snippet. Over time, I switched to the other way of thinking.
I can obviously understand both, but I think the second one a bit more easy to read. Also, the first one is using a local variable which sometimes is a code smell. If there’s a way of not using a local variable (especially one that is a mutable local variable), then I prefer such way.
What are your thoughts on that?
Only this weekend we have the biggest promotion for our Ruby/Rails/React.js books — 40% off with the code BLACKFRIDAY2015
Grab the books here: http://blog.arkency.com/products/