52 lines
1.0 KiB
Ruby
52 lines
1.0 KiB
Ruby
|
module RubyQA
|
||
|
module Interactions
|
||
|
|
||
|
def get_string_response(prompt)
|
||
|
puts "#{prompt}: "
|
||
|
|
||
|
while true
|
||
|
answer = STDIN.gets.strip
|
||
|
|
||
|
if get_user_verification "Is \"#{answer}\" correct?"
|
||
|
return answer
|
||
|
end
|
||
|
end
|
||
|
end
|
||
|
|
||
|
def get_user_verification(prompt)
|
||
|
print "#{prompt} (y/n): "
|
||
|
while true
|
||
|
answer = STDIN.gets.strip.downcase
|
||
|
|
||
|
case answer
|
||
|
when /(y|yes)/
|
||
|
return true
|
||
|
when /(n|no)/
|
||
|
return false
|
||
|
else
|
||
|
puts "#{answer} is not a valid option. Please try again"
|
||
|
end
|
||
|
end
|
||
|
end
|
||
|
|
||
|
def get_integer_response(prompt)
|
||
|
print "#{prompt}: "
|
||
|
while true
|
||
|
answer = STDIN.gets.strip
|
||
|
|
||
|
if not answer =~ /^[[:digit:]]+$/
|
||
|
puts "#{answer} is not a valid number. Please try again"
|
||
|
continue
|
||
|
end
|
||
|
|
||
|
answer = answer.to_i
|
||
|
|
||
|
if get_user_verification "Is #{answer} correct?"
|
||
|
return answer
|
||
|
end
|
||
|
end
|
||
|
end
|
||
|
|
||
|
end
|
||
|
end
|