-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhfr-ch-7-hashes.rb
66 lines (49 loc) · 1.74 KB
/
hfr-ch-7-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
# default hash values, normalizing input
votes = ["Amber Graham", "Brian Martin", "Amber Graham", "Brian Martin", "Brian Martin"]
tally = Hash.new(0)
votes.each do |vote|
tally[vote.upcase] += 1
end
p tally
# hashes as method parameters
puts "*" * 50
def area(options)
options[:length] * options[:width]
end
puts area({length: 2, width: 3})
# hashes as method parameters but with a bigger list of parameters
puts "*" * 50
class Candidate
attr_accessor :name, :age, :occupation, :hobby, :birthplace
def initialize(name, options)
self.name = name
self. age = options[:age]
self.occupation = options[:occupation]
self.hobby = options[:hobby]
self.birthplace = options[:birthplace]
end
end
amy = Candidate.new("Amy Nguyen", {age: 37, occupation: "Engineer", hobby: "Lacrosse", birthplace: "Seattle"})
p amy
# we can leave off the braces as long as the hash argument is the last one
george = Candidate.new("Georgey Porgey", age: 37, occupation: "Engineer", hobby: "Lacrosse", birthplace: "Seattle")
p george
#we run into trouble when we omit the hash parameter which means that we need to set a default
john = Candidate.new("John V", {})
p john
#typos are dangerous as they do not geenrate an error!
michelle = Candidate.new("Michelle Hudson", ageg: 22)
p michelle
#this is where keyword argguments come in! Name and Age are required, everything else is optional
class NewAndImprovedCandidate
attr_accessor :name, :age, :occupation, :hobby, :birthplace
def initialize(name, age:, occupation: nil, hobby: nil, birthplace: nil)
self.name = name
self. age = age
self.occupation = occupation
self.hobby = hobby
self.birthplace = birthplace
end
end
ashik = NewAndImprovedCandidate.new("Ashik V", ageg: 26)
p ashik