Forum: Ruby Arrays - Storing User Input

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
woodyee (Guest)
on 2007-03-09 16:01
(Received via mailing list)
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!!
John J. (Guest)
on 2007-03-09 16:50
(Received via mailing list)
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?
Brian C. (Guest)
on 2007-03-09 16:51
(Received via mailing list)
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.
woodyee (Guest)
on 2007-03-09 17:05
(Received via mailing list)
(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 C. (Guest)
on 2007-03-09 17:33
(Received via mailing list)
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 B. (Guest)
on 2007-03-09 17:34
(Received via mailing list)
perhaps:

something = []
puts "Say something..."
loop do
   nextline = gets.chomp
   something << nextline
   break if nextline == ""
end
puts something
John J. (Guest)
on 2007-03-09 17:36
(Received via mailing list)
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...
This topic is locked and can not be replied to.