Created
August 16, 2019 18:53
-
-
Save rush2sk8/768d1370e0f3f9907aa6c79a5aafeaca to your computer and use it in GitHub Desktop.
gets all values from a hash no matter how nester
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 deep_values(h, acc = []) | |
return [nil] if h.nil? | |
case h | |
when Hash | |
return deep_values(h.values) | |
when Array | |
acc.push(*h.map { |v| deep_values(v) }) | |
else | |
acc.push(h) | |
end | |
acc.flatten | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment