Skip to content

Instantly share code, notes, and snippets.

@theorygeek
Created July 5, 2016 16:19
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 theorygeek/29834c2fbb406f5e029b3fb3116e3372 to your computer and use it in GitHub Desktop.
Save theorygeek/29834c2fbb406f5e029b3fb3116e3372 to your computer and use it in GitHub Desktop.
Finds keys in a hash that are not strings
def detect_bad_keys(obj, parent_path)
result = []
case obj
when Hash
obj.each do |key, value|
if key.is_a?(String)
result.concat(detect_bad_keys(value, [*parent_path, key]))
else
result.push([*parent_path, key])
end
end
when Array
obj.each_with_index do |value, key|
result.concat(detect_bad_keys(value, [*parent_path, key]))
end
end
result
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment