Skip to content

Instantly share code, notes, and snippets.

@nastysloper
Created May 3, 2013 15:26
Show Gist options
  • Save nastysloper/5509858 to your computer and use it in GitHub Desktop.
Save nastysloper/5509858 to your computer and use it in GitHub Desktop.
Hello, all! This exercise is from Zed Shaw's Learn Ruby the Hard Way, and there are a few points that seem mysterious. On line 22, it seems that a hash object is calling a method ".call" with two arguments, the original hash, and a string the user has entered. I know the code works, and I know the code can be simpler. I'm just wondering what sor…
cities = {'CA' => 'San Francisco', 'MI' => 'Detroit', 'FL' => 'Jacksonville'}
cities['NY'] = 'New York'
cities['OR'] = 'Portland'
def find_city(map, state)
if map.include? state
return map[state]
else
return "Not found."
end
end
cities[:find] = method(:find_city)
while true
print "State? (Enter to quit) "
state = gets.chomp
break if state.empty?
puts cities[:find].call(cities, state)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment