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?

1 Answer 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"}]
Sign up to request clarification or add additional context in comments.

7 Comments

I think you posted the output of another version of your code. There is more than one waiter that has the role = "server".
@7stud It is continuous. After "Jack" has been deleted, there is only "Mary".
Restaurant[:waiters].find{|h| h[:name] == "Jack"}[:role] = "manager"
Thanks! Also, how do you print out all the elements in the array since you can't just simply puts it.
Have you considered of accepting an answer before keep adding new questions?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.