"David Douthitt" <DDouthitt / cuna.com> writes:

> Thanks for the help!  Now I'm trying to redefine IO.getc to include
> this behavior.... and not getting very far:
> 
> #!/usr/bin/ruby
> 
> class IO
>    def getc
>       begin
>         system("stty raw -echo")
>         str = self.getc
>       ensure
>         system("stty -raw echo")
>       end
        return str
>    end
> end
> 
> str = STDIN.getc
> p str

You can do this using 'alias'

  class IO
    alias old_getc getc

    def getc
       begin
         system("stty raw -echo")
         str = old_getc
       ensure
         system("stty -raw echo")
       end
    end
 end
 
 str = STDIN.getc
 p str

However, it might be a better idea to use subclassing instead:


  class CharIO < IO
    def getc
       begin
         system("stty raw -echo")
         str = super
       ensure
         system("stty -raw echo")
       end
       return str
    end
  end


Now the only problem is that you're forking two subprocesses for every 
character you read. Depending on your application, this might be
expensive!

You've a couple of options:

1. Write 'enterCharacterMode' and 'exitCharacterMode' methods, and set 
   the mode globally.

2. Investigate the IO#ioctl method.


Have fun


Dave