具有空输入的get.chomp的异常处理

时间:2014-05-06 23:23:02

标签: ruby

我只是在轨道上学习ruby而我正在尝试处理我在程序中提出的异常。我正在按照Codecademy的教程进行操作。

这是我第一次在Stack Overflow上发帖,我做了谷歌搜索并寻找类似的问题但找不到任何东西。

print "Put in an input to daffy duckify the sentence."
user_input = gets.chomp
user_input.downcase!

if user_input.empty?
print "You did not input a string with any characters, please enter some characters  or a string and press enter"
user_input = get.chomp
end

if user_input.include? "s"
    user_input.gsub!(/s/, "th")
    puts"We found an s! ~~~~WOOT!"
else
    print "no s's found in the string inputed"
end
puts "Look at the end string, #{user_input} !"

我试着在输入提示中输入,没有输入任何内容,我收到了这个错误。

未定义的局部变量或方法`get'for#

对我来说这是一个简单的方法来处理这个错误吗?

1 个答案:

答案 0 :(得分:0)

get.chomp更改为gets.chomp(没有get这样的方法

此外,而不是

if user_input.empty?

您可能想要

while user_input.empty?

如果用户继续输入任何内容,您将继续请求用户输入。