Skip to content

Instantly share code, notes, and snippets.

@McTano
Forked from davidvandusen/state_info.rb
Last active May 25, 2016 23:18
Show Gist options
  • Save McTano/ddfe17718a7cfe2c7efd17f33342c53a to your computer and use it in GitHub Desktop.
Save McTano/ddfe17718a7cfe2c7efd17f33342c53a to your computer and use it in GitHub Desktop.
# @states = {
# OR: 'Oregon',
# FL: 'Florida',
# CA: 'California',
# NY: 'New York',
# MI: 'Michigan'
# }
# # Task 1
# @states[:MN] = 'Minnesota'
# @states[:NV] = 'Nevada'
# # Task 2
# @cities = {
# OR: ['Portland', 'Salem', 'Eugene', 'Gresham'],
# FL: ['Jacksonville', 'Miami', 'Tampa', 'St. Petersburg'],
# CA: ['Los Angeles', 'San Diego', 'Santa Clara', 'San Francisco'],
# NY: ['New York', 'Buffalo', 'Rochester', 'Yonkers'],
# MI: ['Detroit', 'Grand Rapids', 'Warren', 'Sterling Heights'],
# MN: ['Minneapolis', 'St. Paul', 'Rochester', 'Bloomington'],
# NV: ['Las Vegas', 'Henderson', 'Reno', 'North Las Vegas']
# }
# # Task 3
# def describe_state(state_code)
# state = @states[state_code.to_state_code.to_sym]
# "'#{state_code.to_sym}' stands for #{state}. "\
# "The 4 largest cities in #{state} are: #{@cities[state_code.to_sym].join(', ')}."
# end
# # Task 4
# @taxes = {
# OR: 0.00,
# FL: 0.06,
# CA: 0.075,
# NY: 0.07,
# MI: 0.06,
# MN: 0.06875,
# NV: 0.0685
# }
# # Task 5
# def calculate_tax(state_code, price)
# (price + (price * @taxes[state_code])).round(2)
# end
# # Task 6
# def find_state_for_city(city)
# @states.each do |key, array|
# return key if array.include?(city)
# end
# "City not found."
# end
# refactored code starts here:
@states = {
OR: {name: 'Oregon', cities: ['Portland', 'Salem', 'Eugene', 'Gresham'], tax: 0.00},
FL: {name: 'Florida', cities: ['Jacksonville', 'Miami', 'Tampa', 'St. Petersburg'], tax: 0.06},
CA: {name: 'California', cities: ['Los Angeles', 'San Diego', 'Santa Clara'], tax: 0.075},
NY: {name: 'New York', cities: ['New York', 'Buffalo', 'Rochester', 'Yonkers'], tax: 0.07},
MI: {name: 'Michigan', cities: ['Detroit', 'Grand Rapids', 'Warren', 'Sterling Heights'], tax: 0.06},
MN: {name: 'Minnesota', cities: ['Minneapolis', 'St. Paul', 'Rochester', 'Bloomington'], tax: 0.06875},
NV: {name: 'Nevada', cities: ['Las Vegas', 'Henderson', 'Reno', 'North Las Vegas'], tax: 0.0685}
}
# I put exceptions in the methods, so hopefully this won't get accessed.
@states.default = {name: "Unknown", cities: ["Unknown"], tax: "Unknown"}
def describe_state(state_code)
sym = state_code.to_sym
state = @states[sym]
name = state[:name]
cities = state[:cities]
if @states.include?(sym)
"'#{sym}' stands for #{name}. "\
"The #{cities.length} largest cities in #{name} are: #{cities.join(', ')}."
else
"State not found."
end
end
def calculate_tax(state_code, price)
sym = state_code.to_sym
state = @states[sym]
tax = state[:tax]
if price.is_a? (Numeric)
return "State not found." unless @states.include?(sym)
(price + (price * tax)).round(2)
else
raise "price is not a number"
end
end
def find_state_for_city(city)
@states.each do |key, hash|
return key if hash[:cities].include?(city)
end
"City not found."
end
def tests
puts describe_state(:CA)
puts describe_state('NY')
puts describe_state('MI')
puts describe_state('HI')
# puts calculate_tax(:CA, "LLLL")
puts calculate_tax("NY", 100)
puts calculate_tax(:MI, 100)
puts calculate_tax(:MN, 34.75)
puts calculate_tax('HI', 85)
puts "Where's Eugene?"
puts find_state_for_city("Eugene")
puts "Where's Edugene?"
puts find_state_for_city("Edugene")
puts "Where's Las Vegas"
puts find_state_for_city("Las Vegas")
end
# tests
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment