From: Rico Knoop on
i'v bin programming for about 2 week now and just about completed
http://pine.fm/LearnToProgram/

but for a change of pace i started something of my own but i cant seem
to get my program to wait X-seconds for user input.

basicly what i got is:

input= "empty"
input= gets.chomp

if input == "empty"
puts "no input"
else
puts input
end

all i want it to do is wait like 10 seconds and if there has bin no user
input it should just continue with input= "empty"

i feel like i'm missing something real simple

thnx in advance

-Bakakyoo-
--
Posted via http://www.ruby-forum.com/.

From: Shu Cao on
results = select [STDIN], nil, nil, X-seconds
if !results
puts "no input"
else
puts gets.chomp
end

On Fri, Mar 26, 2010 at 5:17 PM, Rico Knoop <jointgek(a)gmail.com> wrote:
> i'v bin programming for about 2 week now and just about completed
> http://pine.fm/LearnToProgram/
>
> but for a change of pace i started something of my own but i cant seem
> to get my program to wait X-seconds for user input.
>
> basicly what i got is:
>
> input= "empty"
> input= gets.chomp
>
> if input == "empty"
>  puts "no input"
> else
>  puts input
> end
>
> all i want it to do is wait like 10 seconds and if there has bin no user
> input it should just continue with input= "empty"
>
> i feel like i'm missing something real simple
>
> thnx in advance
>
> -Bakakyoo-
> --
> Posted via http://www.ruby-forum.com/.
>
>

From: Jesús Gabriel y Galán on
On Fri, Mar 26, 2010 at 10:17 AM, Rico Knoop <jointgek(a)gmail.com> wrote:
> i'v bin programming for about 2 week now and just about completed
> http://pine.fm/LearnToProgram/
>
> but for a change of pace i started something of my own but i cant seem
> to get my program to wait X-seconds for user input.
>
> basicly what i got is:
>
> input= "empty"
> input= gets.chomp
>
> if input == "empty"
>  puts "no input"
> else
>  puts input
> end
>
> all i want it to do is wait like 10 seconds and if there has bin no user
> input it should just continue with input= "empty"
>
> i feel like i'm missing something real simple

Timeout is in the standard lib:

require 'timeout'

begin
answer = Timeout::timeout(5) do
gets
end
rescue Timeout::Error
answer = "empty"
end

puts answer

Jesus.

From: Rico Knoop on
Jesús Gabriel y Galán wrote:
> On Fri, Mar 26, 2010 at 10:17 AM, Rico Knoop <jointgek(a)gmail.com> wrote:
>>
>> if input == "empty"
>> �puts "no input"
>> else
>> �puts input
>> end
>>
>> all i want it to do is wait like 10 seconds and if there has bin no user
>> input it should just continue with input= "empty"
>>
>> i feel like i'm missing something real simple
>
> Timeout is in the standard lib:
>
> require 'timeout'
>
> begin
> answer = Timeout::timeout(5) do
> gets
> end
> rescue Timeout::Error
> answer = "empty"
> end
>
> puts answer
>
> Jesus.


it didn't seem to work,
it just kept waiting for me to input something.
i even tried to run just that code.
its still blinking, waiting for input on the background.

am i doing something wrong?
--
Posted via http://www.ruby-forum.com/.

From: Colin Bartlett on
On Fri, Mar 26, 2010 at 11:59 AM, Rico Knoop <jointgek(a)gmail.com> wrote:

> it didn't seem to work,
> it just kept waiting for me to input something.
> i even tried to run just that code.
> its still blinking, waiting for input on the background.
>
> am i doing something wrong?

It might depend on which operating system and/or Ruby version you are using.
I tried the code underneath on Windows Vista, using the Ruby Windows versions,
and Ruby. It works for me using Ruby 1.9 for Windows.
But using Ruby 1.8.6 and using the JRuby 1.8 equivalent seemed to work,
or rather not work, in the way you described in your latest post.

Colin Bartlett