access elements in Ruby nested hash
I am trying to access the elements in a nested hash where keys are similar
symbols.
favs = {
:art => "painters",
:survey1 => [
{:name=>"Josh", :painter=>"Dali" },{:name=>"Mona", :painter=>"Monet"}
],
:survey2 => [
{:name => "Leon", :answer => "None"},
{:name=>"Port", :answer => "Picasso"},
]
}
Q1: Delete Leon-
I came up with this:
favs[:survey2].each { |hash|
hash.delete_if { |k,v|
v=="Leon"
}
}
but I couldn't figure out how to tie the second key value pair in (the
answer/painter) after removing just the name.
Q2 Return Josh's favorite painter - same problem, I can find :name=>Josh
but not sure how to return corresponding painter.
Thanks in advance
No comments:
Post a Comment