Skip to content

Instantly share code, notes, and snippets.

@topher6345
Created March 2, 2016 07:10
Show Gist options
  • Save topher6345/cdd1f41c3053e8f664ee to your computer and use it in GitHub Desktop.
Save topher6345/cdd1f41c3053e8f664ee to your computer and use it in GitHub Desktop.
# Replace Keys K of a Hash H with Keys K'
# Where L is an unordered superset of K with possible duplicates
hash = { 'foo-1' => { data: 102 } , 'bar-1' => { data: 109 } , 'baz-1' => { data: 12 } }
array = ['bar-2', 'foo-2', 'baz-2', 'fab-3', 'gib-2', 'gib-2', 'yup-3']
compare = -> o { o[0][/\w+/] != o[1][/\w+/] }
key_map = array.zip(hash.keys.cycle).flatten.sort.each_slice(2).to_a.reject(&compare).to_h
apply_new_key = -> k, v { [key_map[k], v] }
hash.map(&apply_new_key).to_h
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment