Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
26
Task/Menu/Ruby/menu.rb
Normal file
26
Task/Menu/Ruby/menu.rb
Normal file
|
|
@ -0,0 +1,26 @@
|
|||
def select(prompt, items = [])
|
||||
if items.empty?
|
||||
''
|
||||
else
|
||||
answer = -1
|
||||
until (0...items.length).cover?(answer)
|
||||
items.each_with_index {|i,j| puts "#{j}. #{i}"}
|
||||
print "#{prompt}: "
|
||||
begin
|
||||
answer = Integer(gets)
|
||||
rescue ArgumentError
|
||||
redo
|
||||
end
|
||||
end
|
||||
items[answer]
|
||||
end
|
||||
end
|
||||
|
||||
# test empty list
|
||||
response = select('Which is empty')
|
||||
puts "empty list returns: >#{response}<\n"
|
||||
|
||||
# "real" test
|
||||
items = ['fee fie', 'huff and puff', 'mirror mirror', 'tick tock']
|
||||
response = select('Which is from the three pigs', items)
|
||||
puts "you chose: >#{response}<"
|
||||
Loading…
Add table
Add a link
Reference in a new issue