Skip to content

Instantly share code, notes, and snippets.

@dmitryfry
Last active February 19, 2018 16:59
Show Gist options
  • Save dmitryfry/c7d0d33efdb78e6104ac8c606a6f7d3d to your computer and use it in GitHub Desktop.
Save dmitryfry/c7d0d33efdb78e6104ac8c606a6f7d3d to your computer and use it in GitHub Desktop.
src = [
{key: 'key1', value: 'value1', data: 'data1'},
{key: 'key2', value: 'value2', data: 'data2'},
{key: 'key3', value: 'value3', data: 'data3'}]
part = [
{key: 'key3', data: 'new_data3'},
{key: 'key2', value: 'new_value2'}]
#target = [
# {key: 'key1', value: 'value1', data: 'data1'},
# {key: 'key2', value: 'new_value2', data: 'data2'},
# {key: 'key3', value: 'value3', data: 'new_data3'}]
def merge(src, part)
target = src.dup
part.each { |part_h| target.detect { |target_h| target_h[:key] == part_h[:key] }&.merge!(part_h) }
target
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment