/ Published in: Ruby
Learn Ruby the Hard Way
Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
cities = {'CA' => 'San Francisco', 'MI' => 'Detroit', 'FL' => 'Jacksonville'} cities['NY'] = 'New York' cities['OR'] = 'Portland' def find(list, state) #state is index in hash map if list.include? state return list[state] else return "Not found." end end # ok pay attention! cities[:seek] = method(:find) #put function as proc in methods of hash cities, where index is :seek while true print "State? (ENTER to quit) " state = gets.chomp break if state.empty? # this line is the most important ever! study! puts cities[:seek].call(cities, state) end
URL: http://ruby.learncodethehardway.org/book/ex41.html