Skip to content

Instantly share code, notes, and snippets.

@gbrl
Forked from davidvandusen/state_info.rb
Last active May 1, 2016 19:06
Show Gist options
  • Save gbrl/617f47f94b99235a857ed99517411730 to your computer and use it in GitHub Desktop.
Save gbrl/617f47f94b99235a857ed99517411730 to your computer and use it in GitHub Desktop.
@states = {
OR: 'Oregon',
FL: 'Florida',
CA: 'California',
NY: 'New York',
MI: 'Michigan'
}
@states[:OH] = 'Ohio'
@states[:NC] = 'North Carolina'
@states[:AK] = 'Alaska'
@cities = {
FL: ["Miami", "Clearwater", "Boca Raton"],
NY: ["Brooklyn", "Manhattan", "Albany"],
CA: ["Freso", "Los Angeles", "Palo Alto", "Berkeley"],
AK: ["Nome", "Ankorage", "Juneau"],
OH: ["Columbus"],
MI: ["Detroit", "Flint"],
NC: ["Charlotte", "Raleigh", "Asheville"],
OR: ["Portland", "Eugene", "Salem"]
}
def describe_state(state_code)
answer = ""
if state_code.length != 2
answer = "Your state code is not valid. "
elsif @states[state_code].nil?
answer = "Sorry we don't have any info on that state."
else
answer = "#{state_code} is for #{@states[state_code]}. It has #{@cities[state_code].length} #{@cities[state_code].length > 1 ? 'cities' : 'city'}: #{@cities[state_code].join(", ")}."
end
answer
end
puts describe_state(:NY)
puts describe_state(:OH)
puts describe_state(:CA)
puts describe_state(:AK)
# Pass state code that doesn't exist
puts describe_state(:XY)
puts "----------------------------------------------------"
@taxes = {
OR: 7.5,
FL: 11,
CA: 11.5,
NY: 12,
MI: 9.5,
OH: 8.75,
NC: 10,
AK: 10
}
def calculate_tax(state_code,amount)
state_code = state_code.to_sym
if @states[state_code].nil?
answer = "Sorry we don't have a tax rate for that state."
else
tax_rate = @taxes[state_code].to_f
taxes = amount * (tax_rate / 100)
dollar_amount = "$" << sprintf('%.2f', taxes)
end
end
puts calculate_tax(:OR,20)
puts calculate_tax(:FL,33000)
puts calculate_tax(:AK,5000)
# Pass non-symbol
puts calculate_tax("NY",200)
# Pass symbol that doesn't match a state
puts calculate_tax(:DERP,5000)
puts "----------------------------------------------------"
def find_state_for_city(name)
state_code = "Sorry we couldn't find a state with that city in it."
@cities.each do |state|
cities = state[1]
if cities.include?(name)
state_code = state[0]
end
end
return state_code
end
puts find_state_for_city("Miami")
puts find_state_for_city("Juneau")
puts find_state_for_city("Flint")
# Pass a city name that doesn't exist.
puts find_state_for_city("Derp")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment