Skip to content

Instantly share code, notes, and snippets.

@dmitryfry
Last active February 20, 2018 11:01
Show Gist options
  • Save dmitryfry/37dd37692fce1886ec86342d0408d577 to your computer and use it in GitHub Desktop.
Save dmitryfry/37dd37692fce1886ec86342d0408d577 to your computer and use it in GitHub Desktop.
def merge(src, part)
src.map do |src_item|
new_item = src_item.dup
part.each do |part_item|
new_item.merge! part_item if part_item[:key] == src_item[:key]
end
new_item
end
end
# или если ключи повторяються
def merge(src = [], part = [])
rest = src.size == 0 ? part.map(&:dup) : []
result = src.map do |src_item|
new_item = src_item.dup
part.each do |part_item|
if part_item[:key] == src_item[:key]
new_item.merge! part_item.dup
else
rest << part_item.dup
end
end
new_item
end
result.concat rest
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment