0

I'm having a hard time with nested hashes

 Restaurant = {:name=>"McDonalds",
 :location=>"NYC",
 :chefs=>
  [{:name=>"Sunny", :food=>"fries"},
   {:name=>"Brooklyn", :food=>"burgers"},
   {:name=>"Mac", :food=>"burgers"}],
 :waiters=>
  [{:name=>"Jess", :role=>"senior manager"},
   {:name=>"Sam", :role=>"manager"},
   {:name=>"Jack", :role=>"server"},
   {:name=>"Mary", :role=>"server"}]}

how would I delete the waiter hash with Jack as a value for example? or return all waiter's names with the role of server?

4

1 回答 1

2
Restaurant[:waiters].reject!{|h| h[:name] == "Jack"}
Restaurant # => {
  :name=>"McDonalds", :location=>"NYC",
  :chefs=>[{:name=>"Sunny", :food=>"fries"}, {:name=>"Brooklyn", :food=>"burgers"}, {:name=>"Mac", :food=>"burgers"}],
  :waiters=>[{:name=>"Jess", :role=>"senior manager"}, {:name=>"Sam", :role=>"manager"}, {:name=>"Mary", :role=>"server"}]
}

Restaurant[:waiters].select{|h| h[:role] == "server"}
# => [{:name=>"Mary", :role=>"server"}]
于 2013-07-26T04:10:20.440 回答