Last active
February 20, 2018 11:01
-
-
Save dmitryfry/37dd37692fce1886ec86342d0408d577 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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