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)
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.
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}”
David
···
On Thu, 9 Jan 2003, TOTO wrote:
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.)
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…
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.)
? 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: