From: Roger Pack on
It would be quite convenient to have a getch method in Ruby.
Any opposition to my suggesting its addition to core?
-r
--
Posted via http://www.ruby-forum.com/.

From: Nobuyoshi Nakada on
Roger Pack wrote:
> It would be quite convenient to have a getch method in Ruby.
> Any opposition to my suggesting its addition to core?
> -r

$ /usr/local/bin/ruby-1.9.3 -v -rio/console -e 'p STDIN.gets; p
STDIN.noecho{|f|f.gets("!")}; p STDIN.getch'
ruby 1.9.3dev (2010-07-06 trunk 28554) [x86_64-linux]
hello
"hello\n"
"world!"
"\n"

--
Nobu Nakada
--
Posted via http://www.ruby-forum.com/.

From: botp on
On Tue, Jul 6, 2010 at 2:43 PM, Nobuyoshi Nakada <nobu(a)ruby-lang.org> wrote:
> $ /usr/local/bin/ruby-1.9.3 -v -rio/console -e 'p STDIN.gets; p
> STDIN.noecho{|f|f.gets("!")}; p STDIN.getch'
> ruby 1.9.3dev (2010-07-06 trunk 28554) [x86_64-linux]
> hello
> "hello\n"
> "world!"
> "\n"

uber cool. thank for the tip, nobu.
best regards -botp

From: Roger Pack on

> $ /usr/local/bin/ruby-1.9.3 -v -rio/console -e 'p STDIN.gets; p
> STDIN.noecho{|f|f.gets("!")}; p STDIN.getch'
> ruby 1.9.3dev (2010-07-06 trunk 28554) [x86_64-linux]
> hello
> "hello\n"
> "world!"
> "\n"

Excellent. I'm glad this was added then.

This appears to not work on windows is that expected?
-r
--
Posted via http://www.ruby-forum.com/.

From: Roger Pack on
Oops I missed a require:

>> require 'io/console'
=> true
>> STDIN.getch

> Excellent. I'm glad this was added then.
>
> This appears to not work on windows is that expected?

It does work, and, though it doesn't get arrow keys, it's good enough
for my intents and purposes.

NB if you *do* want arrow keys you'll want to wrap _getch or _getkbhit,
like rb-readline does. See also [1]..

-r

[1] http://betterlogic.com/roger/?p=2930

For google's sake, here's what it does when you *don't* first require
io/console:

>> STDIN.getch
NoMethodError: undefined method `getch' for #<IO:<STDIN>>
from (irb):1
from C:/installs/ruby_trunk_installed/bin/irb.bat:20:in `<main>'
--
Posted via http://www.ruby-forum.com/.