Skip to content

Instantly share code, notes, and snippets.

@andrius
Last active October 1, 2018 09:07
Show Gist options
  • Save andrius/dac7dd2fd5be7b77a94c293bb9300e93 to your computer and use it in GitHub Desktop.
Save andrius/dac7dd2fd5be7b77a94c293bb9300e93 to your computer and use it in GitHub Desktop.
require 'pp'
def embedded_hash(levels, key, data)
if levels == 0
{key => data}
else
{key => embedded_hash(levels-1, key, data)}.merge(data)
end
end
def dry_hash(hash, drop_keys)
hash.map { |k,v|
{ k => (v.kind_of?(Hash) ? dry_hash(v, drop_keys) : v) } unless k =~ drop_keys
}.compact.reduce(&:merge)
end
h = embedded_hash(10-1, :key, {comment: 'this is comment', data: :values})
pp h
pp dry_hash(h, /^comment/)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment