tags:

views:

3487

answers:

6

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
+1  A: 

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

Paul Betts
Thanks - I had looked at highline but it seems to have some external dependencies and one of my constraints is that this has to work under Windows and Linux in both ruby and jruby.
+1  A: 

I tried your code (under Windows) and it seemed to work fine. What OS are you using?

The Dark
A: 

I also tried your code (under OSX) with ruby 1.8.6 and it worked fine:

:! ruby prompt.rb
Prompt>(sdfsdf)

sdfsdf
Prompt>(default)

default

What do you get when you run the following?

c = gets
b = gets
a = gets
p [ a, b, c ]

I just hit 'Enter' 3x and get

["\n", "\n", "\n"]

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).

rampion
A: 

That's bizarre --- I don't understand why it doesn't work for me then? I have been testing on Windows inside the ActiveState Komodo IDE. Is it possible that this is only a problem inside Komodo?

BTW, thanks so much for the quick responses, I really appreciate it

A: 

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.

FYI - your responses should be comments to various answers, rather than answers themselves. Just a tip :)
rampion
A: 

Why? In particular, the purpose of my last response was to help anybody else who runs into the same problem. (Hopefully somebody else doing a search that includes some of the same keywords will quickly find this thread and my confirmation at the end, thereby saving them the headache)


FYI - your responses should be comments to various answers, rather than answers themselves. Just a tip :) – rampion (Dec 2 at 19:05)

David