# Simultaneous text input/output in a command line game

**URL:** <https://www.ruby-forum.com/t/simultaneous-text-input-output-in-a-command-line-game/66073>\
**Category:** Ruby\
**Created:** [February 12, 2006, 7:08am UTC](https://www.ruby-forum.com/t/simultaneous-text-input-output-in-a-command-line-game/66073 "2006-02-12T07:08:09Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![rubyvic](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/35a633/32.png) [@rubyvic](https://www.ruby-forum.com/u/rubyvic)\
**Post date:** [February 12, 2006, 7:08am UTC](https://www.ruby-forum.com/t/simultaneous-text-input-output-in-a-command-line-game/66073/1 "2006-02-12T07:08:09Z")

</div>

Hi everyone!

I?m currently learning Ruby, and have made this simple number guessing  
game:

* * *

puts “I’m thinking of a number from 0 to 10. What’s your guess?”

my\_number = rand(11)  
user\_guess = gets.chomp.to\_i

until user\_guess == my\_number  
if user\_guess \> my\_number  
puts “Too high.”  
else my\_number \> user\_guess  
puts “Too low.”  
end  
puts “Guess again:”  
user\_guess = gets.chomp.to\_i  
end

puts "Yep, it was " + my\_number.to\_s + “. You win!”

* * *

A friend of mine wrote a Java version which you can see here:  
[http://violasong.com/tonyguessinggame.java](http://violasong.com/tonyguessinggame.java). In addition to what my  
script does, his is able to taunt you with a random message if you don?t  
input any data for 5 seconds :).

I want to try adding that functionality to my script, and am wondering  
if it’s possible in Ruby ? to print a message every x seconds _while_  
it’s waiting for you to input something. If someone could point me in  
the right direction, that would be much appreciated!

Victoria Wang

---

<div class="post-metadata">

**Author:** ![rubyvic](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/35a633/32.png) [@rubyvic](https://www.ruby-forum.com/u/rubyvic)\
**Post date:** [February 12, 2006, 11:38am UTC](https://www.ruby-forum.com/t/simultaneous-text-input-output-in-a-command-line-game/66073/2 "2006-02-12T11:38:30Z")

</div>

As a method without having to worry about threads you could also use:

require ‘timeout’

messages = [“What are you waiting for?”, “I don’t have all day.”, “When  
I said ‘take your time’, I didn’t mean this long!”, “Hurry up and enter  
a value!”]

s = nil  
until s  
begin  
Timeout::timeout(5) do  
puts "Enter a value: "  
s = gets  
end  
rescue Timeout::Error  
puts messages[rand(messages.length).to\_i]  
end  
end

---

<div class="post-metadata">

**Author:** ![rubyvic](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/35a633/32.png) [@rubyvic](https://www.ruby-forum.com/u/rubyvic)\
**Post date:** [February 12, 2006, 7:26pm UTC](https://www.ruby-forum.com/t/simultaneous-text-input-output-in-a-command-line-game/66073/3 "2006-02-12T19:26:09Z")

</div>

On 2/12/06, Timothy G. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> begin  
> Timeout::timeout(5) do  
> puts "Enter a value: "  
> s = gets  
> end  
> rescue Timeout::Error  
> puts messages[rand(messages.length).to\_i]  
> end  
> end

I thought this was a good example, so I tried running your code. It  
doesn’t work using the latest One-Click Installer for Windows (1.8.4).  
The timeout never occurs. It does however work under Fedora Core Linux  
using Ruby 1.8.4.

I wonder if this is a Ruby problem or a One-Click Installer problem.
