Stefano Crocco
3/9/2007 3:33:00 PM
Alle venerdì 9 marzo 2007, woodyee ha scritto:
> (blushing) Yes; I want to press Enter.
>
> I want to be able to press Enter without having typed anything in
> order to get back what I typed. The way it is now, I'm only able to
> enter info on 1 line. I want to be able to type, hit enter, type
> something and hit enter, etc, hit enter without typing and then I'll
> see all of those lines that I typed.
If I understand correctly, the behavior of your program should be like this:
> Say something
text1 [press enter]
> Say something
text2 [press enter]
> Say something
[press enter]
> text1
> text2
Then, the following code should do the job:
something=[]
loop do #iterates until explicitly told to stop
puts "Say something..."
res=gets.chomp #read the line from the keyboard
if res.empty? #if the line is empty, the user has only pressed enter, so
puts something #display what he already wrote
break #exit the loop
else something << res #the user has entered some text, so store it
end
end
Essentially, your code had two problems:
1- it lacked the enclosing loop, so the code would have been executed only
once
2- instead of storing the string into the array, you replaced the array with
the string
I hope this helps