Last active
January 18, 2020 17:27
-
-
Save amirrajan/02596a7783a46e044236a1918d65e49f 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
# https://www.codewars.com/kata/521a849a05dd182a09000043 | |
class Hash | |
def collapsed parent_key, key | |
return key unless parent_key | |
val = "#{parent_key}_#{key}" | |
return val.to_s if parent_key.is_a? String | |
return val.to_sym if key.is_a? Symbol | |
return val | |
end | |
def flattened_keys parent_key = nil, accumulator = { }, current = self | |
current.each do |k, v| | |
if current[k].is_a? Hash | |
flattened_keys collapsed(parent_key, k), accumulator, v | |
else | |
accumulator[collapsed(parent_key, k)] = v | |
end | |
end | |
accumulator | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment