Prompt

Is there a Ruby idiom to do prompting? For example,

…/rm.rb: remove `t’? y

?

I find it rather troublesome to do that. I have to use something like:

def prompt(file)
print "#{$0}: remove `#{file}’? "
decision = STDIN.getc
STDIN.getc # Get rid of the ending "\n"
return decision
end

And it is not that safe in fact. If the user enter “Yes\n”, the program will crash.

Can someone suggest a better solution?

Thanks!

Hi –

···

On Thu, 9 Jan 2003, TOTO wrote:

Is there a Ruby idiom to do prompting? For example,

…/rm.rb: remove `t’? y

?

I find it rather troublesome to do that. I have to use something like:

def prompt(file)
print "#{$0}: remove `#{file}'? "
decision = STDIN.getc
STDIN.getc # Get rid of the ending “\n”
return decision
end

And it is not that safe in fact. If the user enter “Yes\n”, the program will crash.

Can someone suggest a better solution?

def prompt(file)
print "#{$0}: remove `#{file}'? "
gets.chomp == ‘y’
end

David


David Alan Black
home: dblack@candle.superlink.net
work: blackdav@shu.edu
Web: http://pirate.shu.edu/~blackdav

def prompt(file)
print "#{$0}: remove `#{file}'? "
STDIN.readline.chomp
end

– Dossy

···

On 2003.01.09, TOTO zhoujing@comp.nus.edu.sg wrote:

Is there a Ruby idiom to do prompting? For example,

…/rm.rb: remove `t’? y

?

I find it rather troublesome to do that. I have to use something like:

def prompt(file)
print "#{$0}: remove `#{file}'? "
decision = STDIN.getc
STDIN.getc # Get rid of the ending “\n”
return decision
end

And it is not that safe in fact. If the user enter “Yes\n”, the program will crash.

Can someone suggest a better solution?


Dossy Shiobara mail: dossy@panoptic.com
Panoptic Computer Network web: http://www.panoptic.com/
“He realized the fastest way to change is to laugh at your own
folly – then you can let go and quickly move on.” (p. 70)

also, couldn’t you do this with the readline module?

brennan

···

On Wednesday, January 8, 2003, at 10:34 AM, TOTO wrote:

Is there a Ruby idiom to do prompting? For example,

…/rm.rb: remove `t’? y

Brennan Leathers digibren@mac.com wrote in message news:67484A65-232D-11D7-9C75-000393C4FE10@mac.com

···

On Wednesday, January 8, 2003, at 10:34 AM, TOTO wrote:

Is there a Ruby idiom to do prompting? For example,

…/rm.rb: remove `t’? y

also, couldn’t you do this with the readline module?

brennan

gets doesn’t work. I tried it at the beginning. I think the problem is
that it Reads the next ``line’’ from the I/O stream. (So it can’t
read the string following the question mark.)

And I thought that readline was simular to gets: Reads a line as with
IO#gets , but raises an EOFError on end of file.

However, it turns out ok. That is quite strange.

Hi –

Brennan Leathers digibren@mac.com wrote in message news:67484A65-232D-11D7-9C75-000393C4FE10@mac.com

Is there a Ruby idiom to do prompting? For example,

…/rm.rb: remove `t’? y

also, couldn’t you do this with the readline module?

brennan

gets doesn’t work. I tried it at the beginning. I think the problem is
that it Reads the next ``line’’ from the I/O stream. (So it can’t
read the string following the question mark.)

And I thought that readline was simular to gets: Reads a line as with
IO#gets , but raises an EOFError on end of file.

However, it turns out ok. That is quite strange.

There must be something else going on with your use of gets. It
should work as expected. Try running this:

print "Enter a word: "
w = gets
puts “You typed #{w}”

Or if you want to go nuts with interpolation:

puts "You typed #{print "Enter a word: “; gets}”

:slight_smile:

David

···

On Thu, 9 Jan 2003, TOTO wrote:

On Wednesday, January 8, 2003, at 10:34 AM, TOTO wrote:


David Alan Black
home: dblack@candle.superlink.net
work: blackdav@shu.edu
Web: http://pirate.shu.edu/~blackdav

Is there a Ruby idiom to do prompting? For example,

…/rm.rb: remove `t’? y

also, couldn’t you do this with the readline module?

brennan

gets doesn’t work. I tried it at the beginning. I think the problem is
that it Reads the next ``line’’ from the I/O stream. (So it can’t
read the string following the question mark.)

huh? so is the following really stupid:

def run
    while (cmd = gets.chomp)
        eval(cmd)
    end
end

? I’m writing a simple little app to introduce myself to ruby doing pretty
much that…

tia
dim

gets() should work, but you should use $stdin.gets() instead:

bash$ ruby -v
ruby 1.6.8 (2002-12-24) [i686-linux]

bash$ ruby -e ‘print "input: "; $stdout.flush; p gets’
input: testing
“testing\n”

bash$ ruby -e ‘print "input: "; $stdout.flush; p gets’ foo
input: -e:1:in `gets’: No such file or directory - “foo” (Errno::ENOENT)
from -e:1

bash$ ruby -e ‘print "input: "; $stdout.flush; p $stdin.gets’ foo
input: testing
“testing\n”

···

On Thu, Jan 09, 2003 at 11:56:00AM +0900, TOTO wrote:

gets doesn’t work. I tried it at the beginning. I think the problem is
that it Reads the next ``line’’ from the I/O stream. (So it can’t
read the string following the question mark.)

oh yeah, missed a line

huh? so is the following really stupid:

def run
    while (cmd = gets.chomp)
            print "# "
        eval(cmd)
    end
end

? I’m writing a simple little app to introduce myself to ruby doing
pretty

···

much that…

tia
dim

Hi –

···

On Thu, 9 Jan 2003, Dmitri Colebatch wrote:

oh yeah, missed a line

huh? so is the following really stupid:

def run
    while (cmd = gets.chomp)
            print "# "
        eval(cmd)
    end
end

? I’m writing a simple little app to introduce myself to ruby doing
pretty
much that…

Have you tried using irb, the interactive Ruby tool? It comes with
the standard Ruby distribution.

David


David Alan Black
home: dblack@candle.superlink.net
work: blackdav@shu.edu
Web: http://pirate.shu.edu/~blackdav

? I’m writing a simple little app to introduce myself to ruby doing
pretty
much that…

Have you tried using irb, the interactive Ruby tool? It comes with
the standard Ruby distribution.

have I tried using it? yes… just for testing expressions etc… have I
thought “hey, I could use that code instead of writing it myself” - no… I
will have a look tonight (o:

thanks
dim