Arrays - Storing User Input


#1

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!!


#2

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?


#3

On Fri, Mar 09, 2007 at 11:00:04PM +0900, woodyee wrote:

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/highline/

Under Windows I have no idea.


#4

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


#5

perhaps:

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


#6

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…


#7

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