Nested Collections

Nested Collections


WarmUp

animals = {"dogs" => 3, "cats" => 5, "iguanas" => 2}
pet_names = ["Fela", "Spot", "Patch", "Willy"]
  • return the count of dogs
  • add 3 parakeets
  • increase the amount of cats by 2
  • add “Claude” to the pet_names
  • access which name is first in the list of pet_names
  • remove “Fela” from the list of pet_names

Nested Arrays

numbers = [[1, 2, 3], [4, 5, 6]]
  • what is numbers.count
  • what is numbers.first.count
  • how can I access the element 5
  • how can I add [7,8,9] to the numbers array

Hashes within an Array

food_feelings = [{:pizza => "tasty"}, {:calzone => "also tasty"}]
  • what is food_feelings.count
  • what is food_feelings.first.count
  • how can I access the element "also tasty"
  • how can I change also tasty to super delicious

Hash within a Hash

pets = {:dog => {:name => "Chance", :weight => "45 pounds"},
        :cat => {:name => "Sassy", :weight => "15 pounds"}}
  • what is pets.count
  • what is pets.keys
  • what is pets.values
  • how can I access the element "15 pounds"
  • how can I add :age => 3 to the value of the key :dog

Array within a Hash

pizza_toppings = {veggies: ["green peppers", "jalapeño", "mushrooms"],
                  protein: ["pepperoni", "sausage", "sardines"],
                  fruit: ["pineapple"]}
  • What is pizza_toppings.count
  • What is pizza_toppings.values
  • How can I access the element "pineapple"
  • How can I add the element "olives" to they key "veggies"

Pair Practice with Hashes and Nesting

See lesson plan.