· 7 years ago · Nov 30, 2018, 04:30 AM
1# Start with the array of hashes
2people = [
3 {
4 "first_name" => "Bob",
5 "last_name" => "Jones",
6 "hobbies" => ["basketball", "chess", "phone tag"]
7 },
8 {
9 "first_name" => "Molly",
10 "last_name" => "Barker",
11 "hobbies" => ["programming", "reading", "jogging"]
12 },
13 {
14 "first_name" => "Kelly",
15 "last_name" => "Miller",
16 "hobbies" => ["cricket", "baking", "stamp collecting"]
17 }
18]
19# Write an "each" loop to print out every person's first and last name on
20# separate lines. The result should be:
21# Bob Jones
22# Molly Barker
23# Kelly Miller
24people.each do |person|
25 puts "#{person["first_name"]} #{person["last_name"]}"
26end
27
28
29# Start with the same array of hashes
30people = [
31 {
32 "first_name" => "Bob",
33 "last_name" => "Jones",
34 "hobbies" => ["basketball", "chess", "phone tag"]
35 },
36 {
37 "first_name" => "Molly",
38 "last_name" => "Barker",
39 "hobbies" => ["programming", "reading", "jogging"]
40 },
41 {
42 "first_name" => "Kelly",
43 "last_name" => "Miller",
44 "hobbies" => ["cricket", "baking", "stamp collecting"]
45 }
46]
47# This time, write an "each" loop to print out each person's hobby, each on
48# separate lines. The result should be:
49# basketball
50# chess
51# phone tag
52# programming
53# reading
54# jogging
55# cricket
56# baking
57# stamp collecting
58people.each do |person|
59 puts person["hobbies"]
60end
61
62
63# Start with the same array of hashes
64people = [
65 {
66 "first_name" => "Bob",
67 "last_name" => "Jones",
68 "hobbies" => ["basketball", "chess", "phone tag"]
69 },
70 {
71 "first_name" => "Molly",
72 "last_name" => "Barker",
73 "hobbies" => ["programming", "reading", "jogging"]
74 },
75 {
76 "first_name" => "Kelly",
77 "last_name" => "Miller",
78 "hobbies" => ["cricket", "baking", "stamp collecting"]
79 }
80]
81=begin
82Use an "each" loop to give each person an email address that consists of their
83first name + last name @ gmail.com. For example, Bob Jones will have an email
84of bobjones@gmail.com. The program should end with: p people so that you can
85see if the correct modifications were made to each hash.
86The result should be:
87[
88 {
89 "first_name" => "Bob",
90 "last_name" => "Jones",
91 "hobbies" => ["basketball", "chess", "phone tag"],
92 "email" => "bobjones@gmail.com"
93 },
94 {
95 "first_name" => "Molly",
96 "last_name" => "Barker",
97 "hobbies" => ["programming", "reading", "jogging"],
98 "email" => "mollybarker@gmail.com"
99 },
100 {
101 "first_name" => "Kelly",
102 "last_name" => "Miller",
103 "hobbies" => ["cricket", "baking", "stamp collecting"],
104 "email" => "kellymiller@gmail.com"
105 }
106]
107# (Note that your output won't be indented nicely).
108=end
109
110people.each do |person|
111 person["email"] = "abc123@gmail.com"
112end
113
114p people