-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathex39.rb
65 lines (53 loc) · 1.4 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
# create a mapping of state to abbreviation
states = {
'Oregon' => 'OR',
'Florida' => 'FL',
'California' => 'CA',
'New York' => 'NY',
'Michigan' => 'MI'
}
# create a basic set of states and some cities in them
cities = {
'CA' => 'San Francisco',
'MI' => 'Detroit',
'FL' => 'Jacksonville'
}
# add some more cities
cities['NY'] = 'New York'
cities['OR'] = 'Portland'
# puts out some cities
puts '-' * 10
puts "NY State has: ", cities['NY']
puts "OR State has: ", cities['OR']
# puts some states
puts '-' * 10
puts "Michigan's abbreviation is:", states['Michigan']
puts "Floroda's abbreviation is:", states['Florida']
# do it by using the state then the cities dict
puts '-' * 10
puts "Michigan has:", cities[states['Michigan']]
puts "Florida has:", cities[states['Florida']]
# puts every state abbreviation
puts '-' * 10
for state, abbrev in states
puts "%s is abbreviated %s" % [state, abbrev]
end
# put every city in state
puts '-' * 10
for abbrev, city in cities
puts "%s has the city %s" % [abbrev, city]
end
# now do both at the same time
puts '-' * 10
for state, abbrev in states
puts "%s state is abbreviated %s and has city %s" % [state, abbrev, cities[abbrev]]
end
puts '-' * 10
# if it's not there you get nil
state = states['Texas']
if not state
puts "Sorry, no Texas."
end
# get a city with a default value
city = cities['TX'] || 'Does Not Exist'
puts "The city for the state 'TX' is: %s" % city