# Loop Timing and Constant Input

**URL:** <https://rubytalk.org/t/loop-timing-and-constant-input/29431>\
**Category:** ruby-talk\
**Created:** [29 July 2006 17:35 UTC](https://rubytalk.org/t/loop-timing-and-constant-input/29431 "2006-07-29T17:35:05Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![Picklegnome](https://avatars.discourse-cdn.com/v4/letter/p/cdc98d/32.png) [@Picklegnome](https://rubytalk.org/u/Picklegnome)\
**Post date:** [29 July 2006 17:35 UTC](https://rubytalk.org/t/loop-timing-and-constant-input/29431/1 "2006-07-29T17:35:05Z")

</div>

A few quick questions:

How can I make a loop that runs at, say, 8Hz? That is, every 1/8 of a  
second, it executes the loop. I could put a pause at the end of the loop  
(what is the syntax for making the script wait a certain amount of time  
before continuing?) but this is rather imprecise - I want the loop to take  
1/8 of a second, including all processing time.

Secondly, how can I get input from the keyboard without the user pressing  
enter? Say, the user is holding "R" and presses "K" while still holding down  
"R". Can I get these key presses ("R"s, a "K", and more "R"s), dump them  
into a buffer, sort out the unique ones (I know how to do this part) and  
pass each of the unique key presses to the appropriate handlers?

Thanks,  
Picklegnome

---

<div class="post-metadata">

**Author:** ![W\_James](https://avatars.discourse-cdn.com/v4/letter/w/e274bd/32.png) [@W\_James](https://rubytalk.org/u/W_James)\
**Post date:** [29 July 2006 18:55 UTC](https://rubytalk.org/t/loop-timing-and-constant-input/29431/2 "2006-07-29T18:55:42Z")

</div>

Picklegnome wrote:

> A few quick questions:
> 
> How can I make a loop that runs at, say, 8Hz? That is, every 1/8 of a  
> second, it executes the loop. I could put a pause at the end of the loop  
> (what is the syntax for making the script wait a certain amount of time  
> before continuing?) but this is rather imprecise - I want the loop to take  
> 1/8 of a second, including all processing time.

span = 1.0 / 8

99.times {  
&nbsp;&nbsp;start\_time = Time.now  
&nbsp;&nbsp;print "."  
&nbsp;&nbsp;sleep span - (Time.now - start\_time)  
}

> Secondly, how can I get input from the keyboard without the user pressing  
> enter?

"What should I feed my pet?" The answer would depend upon what  
kind of pet you have.

"What sort of glue should I use to repair my broken chess piece?"  
That may depend upon the material of which the piece is made.

For windoze:

require 'Win32API'  
Kbhit = Win32API.new("msvcrt", "\_kbhit", , 'I')  
Getch = Win32API.new("msvcrt", "\_getch", , 'I')  
def get\_key  
&nbsp;&nbsp;sleep 0.02  
&nbsp;&nbsp;return nil if Kbhit.call.zero?  
&nbsp;&nbsp;c = Getch.call  
&nbsp;&nbsp;c = Getch.call + 256 if c.zero? || c == 0xE0  
&nbsp;&nbsp;c  
end

---

<div class="post-metadata">

**Author:** ![Eric\_Armstrong](https://avatars.discourse-cdn.com/v4/letter/e/46a35a/32.png) [@Eric\_Armstrong](https://rubytalk.org/u/Eric_Armstrong)\
**Post date:** [1 August 2006 03:51 UTC](https://rubytalk.org/t/loop-timing-and-constant-input/29431/3 "2006-08-01T03:51:52Z")

</div>

Picklegnome wrote:

> how can I get input from the keyboard without the user pressing enter?

\>  
For a platform independent operation, I recently  
began investigating "Curses":

&nbsp;&nbsp;&nbsp;require 'curses'  
&nbsp;&nbsp;&nbsp;include Curses

operations to play with:  
&nbsp;&nbsp;&nbsp;&nbsp;clear -- clear the screen  
&nbsp;&nbsp;&nbsp;&nbsp;addstr -- append text to the buffer  
&nbsp;&nbsp;&nbsp;&nbsp;refresh -- display the text in the buffer  
&nbsp;&nbsp;&nbsp;&nbsp;getch -- get a character  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(compare to ?x, for example, to  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;test for the letter "x")

I have a getting-started template, but neglected  
to push it to the web. Ask me again tomorrow...

So far, I've found two problems with curses:

&nbsp;&nbsp;&nbsp;a) There doesn't seem to be any equivalent for the  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;windows Kbhit. That's the nice little operation  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;that lets you ask whether there is a character  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;in the buffer, and proceed if there isn't any.

&nbsp;&nbsp;&nbsp;b) Examining the Curses API at  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;[RDoc Documentation](http://www.ruby-doc.org/stdlib/), I found what  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;can only be described as an embarassing lack of  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;commentary. Not even one word! And since it's  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;a wrapper, the implementation code is of no help.  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(And curses isn't even mentioned here:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;[http://www.rubymanual.org/list/classes\](http://www.rubymanual.org/list/classes%5C))

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;I can only assume that some form of standard  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;operational documentation exists on the web.  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;If so the API doc should point to it, at least.  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(It's difficult to find, assuming it exists,  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;because of all the references to NCURSES. That  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;seems to be good, but the APIs are a wrapper  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;for an ncurses.lib, which raises the ugly spectre  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;of installation issues in my non-standard  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;location. When I tried that with htree, I got  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;horrible ".so" not found errors that no one  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;could help with--so I abandoned htree for another  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;day.)

Searching on "curses API", I did find these papers:

Curses in Python

> **[ActiveState Community](https://community.activestate.com)**
>
> Community support for users of the ActiveState Platform.

Tutorial on Python Curses Programming

> **[PyCurses.pdf](https://heather.cs.ucdavis.edu/matloff/public_html/Python/PyCurses.pdf)**
>
> 91.16 KB

On my solaris box there is also man page for curses,  
but it lists dozens of APIs that aren't in the ruby library--and to find out what they do, you have to look  
them up individually.

Still, \*nix users have a way to find out what the  
APIs are expected to do. Windows users are SOL, unless  
someone can point me to a good man-page =\> html conversion  
tool. (The utility I wrote goes in the other direction,  
unfortunately.)

---

<div class="post-metadata">

**Author:** ![Morton\_Goldberg](https://avatars.discourse-cdn.com/v4/letter/m/7ea924/32.png) [@Morton\_Goldberg](https://rubytalk.org/u/Morton_Goldberg)\
**Post date:** [1 August 2006 06:09 UTC](https://rubytalk.org/t/loop-timing-and-constant-input/29431/4 "2006-08-01T06:09:00Z")

</div>

I, too, have been looking into Curses. And I agree with what you say about the difficulty in finding reliable tutorials and documentation.

> &nbsp;&nbsp;b) Examining the Curses API at  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;[RDoc Documentation](http://www.ruby-doc.org/stdlib/), I found what  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;can only be described as an embarrassing lack of  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;commentary. Not even one word! And since it's  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;a wrapper, the implementation code is of no help.  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(And curses isn't even mentioned here:  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;[http://www.rubymanual.org/list/classes\](http://www.rubymanual.org/list/classes%5C))

I have issues with this web documentation. I'm using ruby 1.8.2 on OS X and what I see returned from

&nbsp;&nbsp;&nbsp;&nbsp;Curses.methods(false).sort  
&nbsp;&nbsp;&nbsp;&nbsp;Curses::Window.instance\_methods(false).sort

is not the same as the methods described on the ruby-doc site. I don't know if the web documentation describes a newer or an older version than the one I'm using, but it certainly describes a different version.

> On my solaris box there is also man page for curses, but it lists  
> dozens of APIs that aren't in the ruby library--and to find out  
> what they do, you have to look them up individually.

You can always use multiple terminal windows or emacs to view multiple man pages simultaneously.  
Emacs lets you save or print formatted man pages for future reference. On OS X, XCode converts man pages to good-looking HTML.

Regards, Morton

> **···**
>
> On Jul 31, 2006, at 11:51 PM, Eric Armstrong wrote:

---

<div class="post-metadata">

**Author:** ![James\_Edward\_Gray\_II](https://avatars.discourse-cdn.com/v4/letter/j/ea5d25/32.png) [@James\_Edward\_Gray\_II](https://rubytalk.org/u/James_Edward_Gray_II)\
**Post date:** [1 August 2006 13:40 UTC](https://rubytalk.org/t/loop-timing-and-constant-input/29431/5 "2006-08-01T13:40:43Z")

</div>

Would the standard io/wait library work for this? It adds a ready?() method to IO objects.

James Edward Gray II

> **···**
>
> On Jul 31, 2006, at 10:51 PM, Eric Armstrong wrote:
> 
> > &nbsp;&nbsp;a) There doesn't seem to be any equivalent for the  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;windows Kbhit. That's the nice little operation  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;that lets you ask whether there is a character  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;in the buffer, and proceed if there isn't any.

---

<div class="post-metadata">

**Author:** ![Michael\_W\_Ryder1](https://avatars.discourse-cdn.com/v4/letter/m/8e7dd6/32.png) [@Michael\_W\_Ryder1](https://rubytalk.org/u/Michael_W_Ryder1)\
**Post date:** [2 August 2006 22:05 UTC](https://rubytalk.org/t/loop-timing-and-constant-input/29431/6 "2006-08-02T22:05:05Z")

</div>

Eric Armstrong wrote:

> Picklegnome wrote:
> 
> > how can I get input from the keyboard without the user pressing enter?
> 
> \>  
> For a platform independent operation, I recently  
> began investigating "Curses":
> 
> &nbsp;&nbsp;require 'curses'  
> &nbsp;&nbsp;include Curses
> 
> operations to play with:  
> &nbsp;&nbsp;&nbsp;clear -- clear the screen  
> &nbsp;&nbsp;&nbsp;addstr -- append text to the buffer  
> &nbsp;&nbsp;&nbsp;refresh -- display the text in the buffer  
> &nbsp;&nbsp;&nbsp;getch -- get a character  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(compare to ?x, for example, to  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;test for the letter "x")
> 
> I have a getting-started template, but neglected  
> to push it to the web. Ask me again tomorrow...
> 
> So far, I've found two problems with curses:
> 
> &nbsp;&nbsp;a) There doesn't seem to be any equivalent for the  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;windows Kbhit. That's the nice little operation  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;that lets you ask whether there is a character  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;in the buffer, and proceed if there isn't any.
> 
> &nbsp;&nbsp;b) Examining the Curses API at  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;[RDoc Documentation](http://www.ruby-doc.org/stdlib/), I found what  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;can only be described as an embarassing lack of  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;commentary. Not even one word! And since it's  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;a wrapper, the implementation code is of no help.  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(And curses isn't even mentioned here:  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;[http://www.rubymanual.org/list/classes\](http://www.rubymanual.org/list/classes%5C))
> 
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;I can only assume that some form of standard  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;operational documentation exists on the web.  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;If so the API doc should point to it, at least.  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(It's difficult to find, assuming it exists,  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;because of all the references to NCURSES. That  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;seems to be good, but the APIs are a wrapper  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;for an ncurses.lib, which raises the ugly spectre  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;of installation issues in my non-standard  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;location. When I tried that with htree, I got  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;horrible ".so" not found errors that no one  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;could help with--so I abandoned htree for another  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;day.)
> 
> Searching on "curses API", I did find these papers:

O'Reilly published "Programming with curses" in 1991 if you can find a copy of the book.

> **···**
>
> > Curses in Python  
> > [ActiveState Community - Boosting coder and team productivity with ready-to-use open source languages and tools.](http://aspn.activestate.com/ASPN/docs/ActivePython/2.3/howto/curses/curses.html)
> > 
> > Tutorial on Python Curses Programming  
> > [http://heather.cs.ucdavis.edu/~matloff/Python/PyCurses.pdf](http://heather.cs.ucdavis.edu/~matloff/Python/PyCurses.pdf)
> > 
> > On my solaris box there is also man page for curses,  
> > but it lists dozens of APIs that aren't in the ruby library--and to find out what they do, you have to look  
> > them up individually.
> > 
> > Still, \*nix users have a way to find out what the  
> > APIs are expected to do. Windows users are SOL, unless  
> > someone can point me to a good man-page =\> html conversion  
> > tool. (The utility I wrote goes in the other direction,  
> > unfortunately.)

---

<div class="post-metadata">

**Author:** ![Eric\_Armstrong](https://avatars.discourse-cdn.com/v4/letter/e/46a35a/32.png) [@Eric\_Armstrong](https://rubytalk.org/u/Eric_Armstrong)\
**Post date:** [1 August 2006 21:51 UTC](https://rubytalk.org/t/loop-timing-and-constant-input/29431/7 "2006-08-01T21:51:45Z")

</div>

James Edward Gray II wrote:

> > &nbsp;&nbsp;a) There doesn't seem to be any equivalent for the  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;windows Kbhit. That's the nice little operation  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;that lets you ask whether there is a character  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;in the buffer, and proceed if there isn't any.
> 
> Would the standard io/wait library work for this?

\> It adds a ready?() method to IO objects.

> 

ooohhhh. Dude! You're giving me chills!

Found the API docs here:  
[http://stdlib.rubyonrails.org/libdoc/io/wait/rdoc/index.html](http://stdlib.rubyonrails.org/libdoc/io/wait/rdoc/index.html)

What do I do to use it? This:

&nbsp;&nbsp;&nbsp;require 'io/wait'

Or is it something like this:

&nbsp;&nbsp;&nbsp;require 'io/wait/lib/nonblock'

Once I get the require clause right, that adds  
the methods to the IO class.

Then it's a matter of figuring out how to use it  
with Curses. I suppose I can do this:

&nbsp;&nbsp;&nbsp;if $stdin.ready? then  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;c = stdscr.getch  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;...

Yes?

And speaking of curses...

I found this nicely formatted rdoc site in New Zealand:  
[http://www.cs.auckland.ac.nz/references/ruby/stdlib/libdoc/curses/rdoc/](http://www.cs.auckland.ac.nz/references/ruby/stdlib/libdoc/curses/rdoc/)

The fascinating thing is, it mentions about twice as  
many APIs as the standard site: [RDoc Documentation](http://www.ruby-doc.org/stdlib/)

The NZ TOC page says it was generated in Summer of 2004,  
so it's doubtful that it's for 1.9 (unless the date is  
wrong).

I wonder what's going on?

> **···**
>
> > On Jul 31, 2006, at 10:51 PM, Eric Armstrong wrote:

---

<div class="post-metadata">

**Author:** ![Ara.T.Howard6](https://avatars.discourse-cdn.com/v4/letter/a/958977/32.png) [@Ara.T.Howard6](https://rubytalk.org/u/Ara.T.Howard6)\
**Post date:** [1 August 2006 21:57 UTC](https://rubytalk.org/t/loop-timing-and-constant-input/29431/8 "2006-08-01T21:57:15Z")

</div>

$stdin.eof? == maybe

fyi.

-a

> **···**
>
> On Wed, 2 Aug 2006, Eric Armstrong wrote:
> 
> > if $stdin.ready? then
> 
> --  
> we can never obtain peace in the outer world until we make peace with  
> ourselves.  
> - h.h. the 14th dali lama

---

<div class="post-metadata">

**Author:** ![James\_Edward\_Gray\_II](https://avatars.discourse-cdn.com/v4/letter/j/ea5d25/32.png) [@James\_Edward\_Gray\_II](https://rubytalk.org/u/James_Edward_Gray_II)\
**Post date:** [1 August 2006 22:02 UTC](https://rubytalk.org/t/loop-timing-and-constant-input/29431/9 "2006-08-01T22:02:00Z")

</div>

> James Edward Gray II wrote:
> 
> > > &nbsp;&nbsp;a) There doesn't seem to be any equivalent for the  
> > > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;windows Kbhit. That's the nice little operation  
> > > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;that lets you ask whether there is a character  
> > > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;in the buffer, and proceed if there isn't any.
> > 
> > Would the standard io/wait library work for this?
> 
> \> It adds a ready?() method to IO objects.  
> ooohhhh. Dude! You're giving me chills!
> 
> Found the API docs here:  
> [http://stdlib.rubyonrails.org/libdoc/io/wait/rdoc/index.html](http://stdlib.rubyonrails.org/libdoc/io/wait/rdoc/index.html)
> 
> What do I do to use it? This:
> 
> &nbsp;&nbsp;require 'io/wait'

That's the one, yes.

> Once I get the require clause right, that adds  
> the methods to the IO class.
> 
> Then it's a matter of figuring out how to use it  
> with Curses. I suppose I can do this:
> 
> &nbsp;&nbsp;if $stdin.ready? then  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;c = stdscr.getch  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;...
> 
> Yes?

I would think so, though I have never used curses.

James Edward Gray II

> **···**
>
> On Aug 1, 2006, at 4:51 PM, Eric Armstrong wrote:
> 
> > > On Jul 31, 2006, at 10:51 PM, Eric Armstrong wrote:
