Q:

Hash not returning values from gets.chomp

For some reason the hash I created doesn't return the value when I try to get argument from the command line.
Here's the code
DIAL_BOOK = {
    "East Bay": 510,
    "San Francisco": 415,
    "San Jose": 650,
}

city = gets.chomp

if city == "East Bay"
   puts "Area Code: #{DIAL_BOOK[city]}       
end

Output Results
Which area code would you like to find out 
East Bay
Area Code:

I'm completely confused on what's going on here. When I hard code "East Bay" to the hash it returns me 510 just fine. Anybody have an suggestion on what's going on?

A:

You're passing in a symbol, :city, instead of the actual variable city.
String interpolation is done with a # and not a $ (e.g. #{DIAL_BOOK[city]}).
Your hash's keys are symbols but city contains a string. Either change your hash to use strings as keys (DIAL_BOOK = { 'Easy Bay' => 510 }) or convert the city to a symbol before lookup (#{DIAL_BOOK[city.to_sym]).

Additionally, you may want to simplify and remove your if condition unless you specifically only want to print the value for "East Bay".

