-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhash_notes.txt
More file actions
49 lines (39 loc) · 1.12 KB
/
hash_notes.txt
File metadata and controls
49 lines (39 loc) · 1.12 KB
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
Hashes
--> Arrays use number (integers) to index the elements in it.
--> Hashes let you use (almost) anything, not just numbers, as your index.
--> Which means it associates one thing to another (key-value pairings - mapping)
Sample hash code in Ruby:
stuff = {'name' => 'Zed', 'age' => 39, 'height' => 6 * 12 + 2}
puts stuff['name']
// prints Zed
puts stuff['age']
// prints 39
puts stuff['height']
// prints 74
stuff['city'] = "San Francisco"
print stuff['city']
// prints San Francisco
--> We can also put new things into the hash with strings.
--> You aren't limited to strings for keys and values.
--> We can also do this:
stuff[1] = "Wow"
stuff[2] = "Neato"
puts stuff[1]
// prints Wow
puts stuff[2]
// prints Neato
stuff
// prints:
// {"name"=>"Zed", "age"=>39, "height"=>74, "city"=>"San Francisco", 1=>"Wow", 2=>"Neato"}
--> Here's how you delete things:
stuff.delete('city')
stuff.delete(1)
stuff.delete(2)
stuff
// prints:
// {"name"=>"Zed", "age"=>39, "height"=>74}
--> You can map to maps, such as in hashing.rb
--> You can default values using ||= with the nil result
--> Ex.:
city = cities['TX']
city ||= 'Does Not Exist'