-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathex40_0.rb
executable file
·39 lines (31 loc) · 936 Bytes
/
ex40_0.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#!/usr/bin/env ruby
# Create hash "cities"
# Add keys and values of states and cities to the hash "cities"
cities = {'CA' => 'San Francisco',
'MI' => 'Detroit',
'FL' => 'Jacksonville'
}
# Add more to the hash
cities['NY'] = 'New York'
cities['OR'] = 'Portland'
# Create a method on ruby called "find_city"
def find_city(cities, state)
if cities.has_key? state
return cities[state]
else
return "Not found."
end
end
# There's a method on ruby called "find_city"
# I'm asking ruby to give me "find_city" as a methodobject
# I'm taking that methodobject and storing it as a value in a hash called "cities" using the symbol :find as the key
cities[:find] = method(:find_city)
# True is always true
# This while loop will run forever unless we break out of it
while true
print "State? (ENTER to quit)"
state = gets.chomp
break if state.empty?
print "Capital: "
puts cities[:find].call(cities, state)
end