Ruby gets 函数不会多次接受空行
我正在尝试编写一个简单的 ruby 函数,它可以提示用户输入一个值,如果用户自己按 ENTER 键,则使用默认值。 在下面的示例中,第一次调用 Prompt 函数可以通过单独按 ENTER 来处理,并且将使用默认值。 但是,第二次我调用 Prompt 并按 ENTER 时,没有任何反应,结果我必须在 ENTER 之前按其他字符才能从“gets”调用返回。
必须有某种方法来刷新输入缓冲区以避免此问题。 有人知道该怎么办吗?
谢谢,
大卫
def BlankString(aString)
return (aString == nil) ||
(aString.strip.length == 0)
end
#Display a message and accept the input
def Prompt(aMessage, defaultReponse = "")
found = false
result = ""
showDefault = BlankString(defaultReponse) ? "" : "(#{defaultReponse})"
while not found
puts "#{aMessage}#{showDefault}"
result = gets.chomp
result.strip!
found = result.length > 0
if !found
then if !BlankString(showDefault)
then
result = defaultReponse
found = true
end
end
end
return result
end
foo = Prompt("Prompt>", "sdfsdf")
puts foo
foo = Prompt("Prompt>", "default")
puts foo
I'm trying to write a simple ruby function that can prompt the user for a value and if the user presses ENTER by itself, then a default value is used.
In the following example, the first call to the Prompt function can be handled by pressing ENTER by itself and the default value will be used. However, the second time I call Prompt and press ENTER, nothing happens, and it turns out I have to press some other character before ENTER to return from the 'gets' call.
There must be some way to flush the input buffer to avoid this problem. Anyone know what to do?
Thanks,
David
def BlankString(aString)
return (aString == nil) ||
(aString.strip.length == 0)
end
#Display a message and accept the input
def Prompt(aMessage, defaultReponse = "")
found = false
result = ""
showDefault = BlankString(defaultReponse) ? "" : "(#{defaultReponse})"
while not found
puts "#{aMessage}#{showDefault}"
result = gets.chomp
result.strip!
found = result.length > 0
if !found
then if !BlankString(showDefault)
then
result = defaultReponse
found = true
end
end
end
return result
end
foo = Prompt("Prompt>", "sdfsdf")
puts foo
foo = Prompt("Prompt>", "default")
puts foo
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
从技术上讲,这不是一个答案,但无论如何它都会帮助您:使用 Highline (http://highline.rubyforge。 org/),如果你正在制作这样的命令行交互界面,它会为你省去很多麻烦
This isn't technically an answer, but it'll help you anyways: use Highline (http://highline.rubyforge.org/), it'll save you a lot of grief if you're making a command-line interactive interface like this
我尝试了你的代码(在 Windows 下),它似乎工作正常。
您使用什么操作系统?
I tried your code (under Windows) and it seemed to work fine.
What OS are you using?
我还用 ruby 1.8.6 尝试了你的代码(在 OSX 下),效果很好:
运行以下命令时你会得到什么?
我只是按了 3 次“Enter”,然后
我猜问题出在你的 while 语句中,没有向它传递一个
defaultResponse
(在某些代码中,你实际上是正在运行,这不在您的示例中)。I also tried your code (under OSX) with ruby 1.8.6 and it worked fine:
What do you get when you run the following?
I just hit 'Enter' 3x and get
I'm guessing what's wrong is that you're entering an infinite loop in your while statement by not passing it a
defaultResponse
(in some code that you're actually runinng that's not in your example).我已经确认(通过在科莫多外部运行该程序)该问题实际上仅发生在科莫多内部。 我感谢大家的反馈并花时间进行独立测试(我没有想到)以帮助缩小问题范围。
I have confirmed (by running the program outside of Komodo) that the problem is in fact solely happening inside Komodo. I thank everyone for the feedback and taking the time to do the independent test (which I hadn't though of) to help narrow down the problem.