Skip to content

Instantly share code, notes, and snippets.

@noqcks
Created September 4, 2014 21:52
Show Gist options
  • Save noqcks/c2cd031a95669dc75166 to your computer and use it in GitHub Desktop.
Save noqcks/c2cd031a95669dc75166 to your computer and use it in GitHub Desktop.
w1d3
$states = {
OR: 'Oregon',
FL: 'Florida',
CA: 'California',
NY: 'New York',
MI: 'Michigan'
}
#1. adding new states to state hash
$states[:TX] = 'Texas'
$states[:KY] = 'Kentucky'
#2. major cities array nested within state hash
$cities = {
OR: [
'Portland',
'Salem'
],
FL: [
'Miami',
'Tampa'
],
CA: [
'Los Angeles',
'San Diego'
]
}
#3. a little description for each state
def describe_state(state_code)
puts "#{state_code} is for #{$states[state_code]}"
puts "It has two major cities: #{$cities[state_code]}"
end
#4. adding taxes hash
$taxes = {
OR: 10.0,
FL: 5.0,
CA: 35.0
}
#5. calculating tax method
def calculate_tax(state_code, dollars)
#determines tax rate to use
tax_rate = $taxes[state_code]
#calculates tax based on the dollar amount and rate
tax_amount = dollars * (tax_rate / 100)
#returns the tax amount rounded to 2 decimal places
tax_amount.round(2)
end
#6. take city name, return state the city is in
def find_state_for_city(city_name)
# Return the state code for where that city is located
$cities.each do |state, city|
return state.to_s if city.include?(city_name)
end
end
puts $states.inspect
puts $cities.inspect
puts describe_state(:OR)
puts calculate_tax(:OR, 124.68).inspect
puts find_state_for_city("Miami").inspect
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment