Last active
October 1, 2018 09:07
-
-
Save andrius/dac7dd2fd5be7b77a94c293bb9300e93 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
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