We need to build a hash, and we need to build it from each element in an array.
Old way
def visitors_name_email
result = {}
visitors.each do |visitor|
result[visitor.name] = visitor.email
end
result
end
New way
def visitors_name_email
visitors.inject({}) do |result, visitor|
result.merge(visitor.name => visitor.email)
end
end
No comments:
Post a Comment