-
Notifications
You must be signed in to change notification settings - Fork 0
/
hash_practice.rb
106 lines (76 loc) · 2.98 KB
/
hash_practice.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# In the below exercises, write code that achieves
# the desired result. To check your work, run this
# file by entering the following command in your terminal:
# `ruby section3/exercises/hashes.rb`
# Example: Write code that prints a hash holding grocery store inventory:
foods = {apples: 23, grapes: 507, eggs: 48}
p foods
# Write code that prints a hash holding zoo animal inventory:
zoo = {zebras: 1, lions: 5, elephants: 8}
p zoo
# Write code that prints all of the 'keys' of the zoo variable
# you created above:
# YOUR CODE HERE
p zoo.keys
# Write code that prints all of the 'values' of the zoo variable
# you created above:
# YOUR CODE HERE
p zoo.values
# Write code that prints the value of the first animal of the zoo variable
# you created above:
# YOUR CODE HERE
p zoo[:zebras]
# Write code that adds an animal to the zoo hash.
# Then, print the updated hash:
# YOUR CODE HERE
zoo[:tigers] = 2
p zoo
#-------------------
# Part 2: Email
#-------------------
# Think about all the pieces of information associated with one single email in your inbox.
# It has a sender, a subject, ...
# Declare a variable that stores hash. Each key should be an attribute of an email and each
# value should be some appropriate value for that key. Work to have at least 5 key-value pairs.
# Write code that prints your email hash to the terminal.
# Write code that prints all of the 'keys' of the email hash
# you created above:
# YOUR CODE HERE
# Write code that prints all of the 'values' of the email hash
# you created above:
# YOUR CODE HERE
#-------------------
# Part 3: Many Emails - OPTIONAL EXTENSION
#-------------------
# LONG EXAMPLE:
# Now that we've learned about Objects AND Arrays, we can combine them.
# Check out the following example of an array of Instagram posts:
# posts = ["image at beach", "holiday party", "adorable puppy", "video of cute baby"];
# Frankly, that was a very simplified version of the Array the Instagram developers have
# written and work with. Still probably slightly simplified as we don't know what their code
# actually looks like, but it may look more like this:
posts = [
{
'image_src' => "./images/beach.png",
'caption' => "At the beach with my besties",
'timestamp' => "4:37 PM August 13, 2019",
'number_likes' => 0,
'comments' => []
},
{
'image_src' => "./images/holiday-party.png",
'caption' => "What a great holiday party omg",
'timestamp' => "11:37 PM December 31, 2019",
'number_likes' => 13,
'comments' => []
}
]
# puts posts
# puts posts[0]
# The code snippet above shows an Array with 2 elements. Each element in an
# Object Literal. Each of those Object Literals has 4 key-value pairs. This may LOOK
# a bit daunting - it's OK! You don't need to be 100% comfortable with this, but it's
# good to have some exposure before going into Mod 1.
# YOU DO: Create an array of at least 3 EMAIL Object Literals, using the same
# key-value pairs you used in your email Object above.
# Then, log the email Array to the console.