Skip to content

Instantly share code, notes, and snippets.

@omarismail
Created March 1, 2015 13:15
Show Gist options
  • Save omarismail/e15a753b3540fb61eefe to your computer and use it in GitHub Desktop.
Save omarismail/e15a753b3540fb61eefe to your computer and use it in GitHub Desktop.
Hash of Hashes
big_hash = {
:random_key => [
1, 2, "three", {
:foo => "bar",
:bar => "foo"
}
],
:another_key => 2,
:yet_another => {
:lorem => {
:abc => {
:bcs => "bcd",
:sad => "foo",
:some => [1, 3, 4, {
:last => "thing",
:is => "this"
}]
}
},
:ipsum => "foo",
:muspi => [{:foo => "bar", :baz => "biz"}, {:me => ["you", "health"]}, {:nums => "123"}]
},
:yup_more => "foo"
}
# write a method that will take in a hash where the values of the hash can be
# other hashes, arrays, strings, or integers, and find all the strings and integers,
# and put them into an array.
@final_array = []
def recursive_method (hash)
hash.each do |key, value|
# 1) if value is string, @final_array.push(value)
# 2) if value is integer, @final_array.push(value)
# 3) if value is array, iterate thru it and find out
# if the values are integer or string.
# 4) if the value in an array is another array, then
# probably best to create another method for handling arrays
# and recursively calling itself to push the string/integers
# into the final array
# if the value is a hash, then call #recursive_method (value)
# this will pass the value (a hash) into the method we defined,
# which will repeat the process over again.
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment