[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Arrays - Storing User Input

woodyee

3/9/2007 1:56:00 PM

Easy one but I can't figure this out! I'm trying to ask someone to
type some stuff, and then at the end (when they press the spacebar
without typing anything), I want to display what they typed.

Here's my code:


something = []
puts "Say something..."
something = gets.chomp
puts something

I'm seeing what I typed after I hit the spacebar. Any ideas? Thanks!!

6 Answers

John Joyce

3/9/2007 2:50:00 PM

0

space bar?
you mean enter or return?
It does exactly what it should do.
you input , it outputs the same thing immediately.
What is it that you are expecting it to do?
On Mar 9, 2007, at 11:00 PM, woodyee wrote:

> Easy one but I can't figure this out! I'm trying to ask someone to
> type some stuff, and then at the end (when they press the spacebar
> without typing anything), I want to display what they typed.
>
> Here's my code:
>
>
> something = []
> puts "Say something..."
> something = gets.chomp
> puts something
>
> I'm seeing what I typed after I hit the spacebar. Any ideas? Thanks!!
>
>


Brian Candler

3/9/2007 2:51:00 PM

0

On Fri, Mar 09, 2007 at 11:00:04PM +0900, woodyee wrote:
> Easy one but I can't figure this out! I'm trying to ask someone to
> type some stuff, and then at the end (when they press the spacebar
> without typing anything), I want to display what they typed.
>
> Here's my code:
>
>
> something = []
> puts "Say something..."
> something = gets.chomp
> puts something
>
> I'm seeing what I typed after I hit the spacebar. Any ideas? Thanks!!

You mean 'Enter' rather than 'Spacebar' ?

For Linux, you can use the ruby-termios library (from RAA) to turn echo
off/on, or highline (also from RAA) for a higher-level interface.
http://raa.ruby-lang.org/project...

Under Windows I have no idea.

woodyee

3/9/2007 3:04:00 PM

0

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

Stefano Crocco

3/9/2007 3:33:00 PM

0

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

John Browning

3/9/2007 3:34:00 PM

0

perhaps:

something = []
puts "Say something..."
loop do
nextline = gets.chomp
something << nextline
break if nextline == ""
end
puts something

On 9 Mar 2007, at 15:05, woodyee wrote:

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


John Joyce

3/9/2007 3:36:00 PM

0

Oh! Well, friend, you need more program.
You need a loop and a prompt or some exit mechanism.
The most basic of a program like this is loop that waits to be told
to quit, or told to do other things.
So you want to put that stuff inside a loop that waits for a
condition to exit and (perhaps another condition to) give output.
here is some pseudo code, you'll do the not-so-heavy lifting...

while condition is
prompt for input
while another condition is
get input
continue or end
get input command for output
prompt for more?
continue or end
end

Pretty much you will see this in any language. Definitely can be done
more gracefully.
Definitely can have more features.
Questions start to arise...
On Mar 10, 2007, at 12:05 AM, woodyee wrote:

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