From: ES Date: 2005-05-15T11:28:28+09:00 Subject: Re: re-opening STDIN after it's been closed? David Vincelli wrote: > Hi, > > On 5/14/05, nobu.nokada@softhome.net wrote: > >>You should alwasy check if get succeeded. >> >>prompt = <>Make your selection from the list below: \n >>a) foo >>b) bar >> >>PROMPT >> >>while (print(prompt); ans = gets) >>handle_action(ans) >>end > > > > I haven't tried this yet but it's definately better than what I originally > posted. > > >>Or even better if you can use readline: >> >>while ans = readline(prompt) >>handle_action(ans) >>end > > > > But I did try this. I don't think your usage of readline is correct. ri > tells me that readline takes a single parameter and it's the line seperator. > > > I suppose you meant this: > > while (print prompt; ans = readline) > handle_action(ans) > end > > No way to do it. If you believe STDIN is connected to tty, you > >>may want to do STDIN.reopen("/dev/tty"). > > > > Thanks for the tip. I'll stick to the fixed up while loop. > Have you taken a look at some libraries to help you with this? Cmd[1] is a simple tool for creating command-line oriented interpreters (think of sqlite/svnadmin/mysqladmin etc.) HighLine[2] provides a higher-level interface to the command-line from your script. E [1] http://rubyforge.org/projects/cmd/ [2] http://rubyforge.org/projects/highline/ -- template void quack(duck& d) { d.quack(); }