Skip to content

Instantly share code, notes, and snippets.

@amirrajan
Last active January 18, 2020 17:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save amirrajan/02596a7783a46e044236a1918d65e49f to your computer and use it in GitHub Desktop.
Save amirrajan/02596a7783a46e044236a1918d65e49f to your computer and use it in GitHub Desktop.
# 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