Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # 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
- people.each do |person|
- puts "#{person["first_name"]} #{person["last_name"]}"
- end
- # 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
- people.each do |person|
- puts person["hobbies"]
- end
- # 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"]
- }
- ]
- =begin
- 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).
- =end
- people.each do |person|
- person["email"] = "abc123@gmail.com"
- end
- p people
Add Comment
Please, Sign In to add comment