Episode 20a - Arrays of Hashes

Exercises

  1. Start with the array of hashes
    people = [
      {
        "first_name" => "Bob",
        "last_name" => "Jones", 
        "hobbies" => ["basketball", "chess", "phone tag"]
       },
       {
        "first_name" => "Molly",
        "last_name" => "Barker",
        "hobbies" => ["programming", "reading", "jogging"]
       },
       {
        "first_name" => "Kelly",
        "last_name" => "Miller",
        "hobbies" => ["cricket", "baking", "stamp collecting"]
       }
    ]
    
    Write an "each" loop to print out every person's first and last name on separate lines. The result should be:
    Bob Jones
    Molly Barker
    Kelly Miller
  2. Start with the same array of hashes
    people = [
      {
        "first_name" => "Bob",
        "last_name" => "Jones", 
        "hobbies" => ["basketball", "chess", "phone tag"]
       },
       {
        "first_name" => "Molly",
        "last_name" => "Barker",
        "hobbies" => ["programming", "reading", "jogging"]
       },
       {
        "first_name" => "Kelly",
        "last_name" => "Miller",
        "hobbies" => ["cricket", "baking", "stamp collecting"]
       }
    ]
    
    This time, write an "each" loop to print out each person's hobby, each on separate lines. The result should be:
    basketball
    chess
    phone tag
    programming
    reading
    jogging
    cricket
    baking
    stamp collecting
  3. Start with the same array of hashes
    people = [
      {
        "first_name" => "Bob",
        "last_name" => "Jones", 
        "hobbies" => ["basketball", "chess", "phone tag"]
       },
       {
        "first_name" => "Molly",
        "last_name" => "Barker",
        "hobbies" => ["programming", "reading", "jogging"]
       },
       {
        "first_name" => "Kelly",
        "last_name" => "Miller",
        "hobbies" => ["cricket", "baking", "stamp collecting"]
       }
    ]
    
    Use an "each" loop to give each person an email address that consists of their first name + last name @ gmail.com. For example, Bob Jones will have an email of bobjones@gmail.com. The program should end with:
    p people
    so that you can see if the correct modifications were made to each hash. The result should be:
    [
      {
        "first_name" => "Bob",
        "last_name" => "Jones", 
        "hobbies" => ["basketball", "chess", "phone tag"],
        "email" => "bobjones@gmail.com"
       },
       {
        "first_name" => "Molly",
        "last_name" => "Barker",
        "hobbies" => ["programming", "reading", "jogging"],
        "email" => "mollybarker@gmail.com"
       },
       {
        "first_name" => "Kelly",
        "last_name" => "Miller",
        "hobbies" => ["cricket", "baking", "stamp collecting"],
        "email" => "kellymiller@gmail.com"
       }
    ]
    (Note that your output won't be indented nicely).