-
Notifications
You must be signed in to change notification settings - Fork 0
/
ruby-hashes.rb
106 lines (78 loc) · 2.73 KB
/
ruby-hashes.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
# RUBY HASHES 3/6/2024
# Hash = data structure, unique key:value pairs.
# key is a symbol, value is any data type
# access value by referencing the key
# 2 ways to create Hash
# 1. k:v pairs inside { }
# pairs are separated by a comma
# assign to variable to print out hash
# person = { first_name: "Buffy", last_name: "Summers", role: "Vampire Slayer" }
# p person
{:first_name=>"Buffy", :last_name=>"Summers", :role=>"Vampire Slayer"}
people = Hash.new
p people
# {}
# Manipulating Hashes
# C - create
# R - read(return data)
# U - update
# D - delete
# CRUD
# Create:
person = { first_name: "Buffy", last_name: "Summers", role: "Vampire Slayer" }
# p person
# Read:
# p person - returns all the data in hash
# return one value
# p person[:first_name]
# "Buffy"
# Add content to Hash
p person[:mother] = "Joyce"
# p person
# {:first_name=>"Buffy", :last_name=>"Summers", :role=>"Vampire Slayer", :mother=>"Joyce"}
# update existing data
person[:role] = "The Chosen One"
# p person
# {:first_name=>"Buffy", :last_name=>"Summers", :role=>"The Chosen One", :mother=>"Joyce"}
person[:love_interests] = ['Angel', 'Spike']
# p person
{:first_name=>"Buffy", :last_name=>"Summers", :role=>"The Chosen One", :mother=>"Joyce", :love_interests=>["Angel", "Spike"]}
# update the key
person[:mom] = person.delete(:mother)
# hashname[:new_key] = hashname.delete(:old_key)
# p person
{:first_name=>"Buffy", :last_name=>"Summers", :role=>"The Chosen One", :love_interests=>["Angel", "Spike"], :mom=>"Joyce"}
#add to existing array
person[:love_interests][2] = 'Riley'
# p person[:love_interests]
# ["Angel", "Spike", "Riley"]
# change value at specific index in array
person[:love_interests][0] = "Angelo"
# p person[:love_interests]
# ["Angelo", "Spike", "Riley"]
# DELETE:
person.delete(:last_name)
# p person
{:first_name=>"Buffy", :role=>"The Chosen One", :love_interests=>["Angelo", "Spike", "Riley"], :mom=>"Joyce"}
# delete element in the array that is a 'value' in k-v pair
person[:love_interests].delete("Riley")
p person
{:first_name=>"Buffy", :role=>"The Chosen One", :love_interests=>["Angelo", "Spike"], :mom=>"Joyce"}
# Enumerables and Duck Typing
# modules - grouping together like things with similar properties
# enumerables - module that groups things that are ITERABLE --> hashes, arrays, ranges, etc
# aka Duck Typing
characters = {slayer: 'Buffy', witch: 'Willow', vampire: 'Spike'}
characters.each do |key, value|
p "#{value}'s role is the #{key}"
end
# "Buffy's role is the slayer"
# "Willow's role is the witch"
# "Spike's role is the vampire"
def buffy_roles hash
hash.map do |key, value|
"#{value} is a #{key}"
end
end
p buffy_roles (characters)
# ["Buffy is a slayer", "Willow is a witch", "Spike is a vampire"] // map returns an array!