Created
July 5, 2016 16:19
-
-
Save theorygeek/29834c2fbb406f5e029b3fb3116e3372 to your computer and use it in GitHub Desktop.
Finds keys in a hash that are not strings
This file contains hidden or 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
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