-
Notifications
You must be signed in to change notification settings - Fork 0
/
ex39.rb
66 lines (55 loc) · 1.47 KB
/
ex39.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
# create a mapping of state to abbreviation
states = {
'Oregon' => 'OR',
'Florida' => 'FL',
'California' => 'CL',
'New York' => 'CA',
'Michigan' => 'MI',
}
# create a basic set of states and some citites in them
citites = {
'CA' => 'San Francisco',
'MI' => 'Detroit',
'FL' => 'Jacksonville',
}
# add some more citites
citites[ 'NY' ] = 'New York'
citites[ 'OR' ] = 'Portland'
# puts out some cities
puts '-' * 10
puts "NY State has: #{citites['NY']}"
puts "OR State has: #{citites['OR']}"
#puts some states
puts '-' * 10
puts "Michigan's abbreviation is: #{states['Michigan']}"
puts "Florida's abbreviation is: #{states['Florida']}"
# do it by using te state then citites dict
puts '-' * 10
puts "Michigan has: #{citites[states['Michigan']]}"
puts "Florida has: #{citites[states['Florida']]}"
# puts every state abbreviation
puts '-' * 10
states.each do |states, abbrev|
puts "#{states} has the abbreviated #{abbrev}"
end
# puts every city in state
puts '-' * 10
citites.each do |abbrev, city|
puts "#{abbrev} has the city #{city}"
end
# now do both at the same time
puts '-' * 10
citites.each do |state, abbrev|
city = citites[abbrev]
puts "#{state} is abbreviated #{abbrev} and has city #{city}"
end
puts '-' * 10
# by default ruby says "nil" when something isn't in there
state = states['Texas']
if !state
puts "sorry, no Texas."
end
# default values using ||= with the nil result
city = citites['TX']
city ||= 'Does Not Exist'
puts "The city for the state 'TX' is: #{city}"