Return to Snippet

Revision: 54193
at December 14, 2011 09:40 by ariellephan


Initial Code
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

Initial URL
http://ruby.learncodethehardway.org/book/ex41.html

Initial Description
Learn Ruby the Hard Way

Initial Title
Function as Procedure in Method called on Hashes

Initial Tags


Initial Language
Ruby