1
0
Fork 0
lrthw/exercises/ex39.rb

56 lines
1.2 KiB
Ruby
Raw Normal View History

2023-09-30 13:20:59 +00:00
# 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"]}"
# do it by using the state then cities dict
puts "-" * 10
puts "Michigan has: #{cities[states["Michigan"]]}"
puts "Florida has: #{cities[states["Florida"]]}"
# puts every city in state
puts "-" * 10
cities.each do |abbrev, city|
puts "#{abbrev} has the city #{city}"
end
# now do both at the same time
puts "-" * 10
states.each do |state, abbrev|
city = cities[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 = cities["TX"]
city ||= "Does not exist"
puts "The city for state 'TX' is: #{city}"