Newbie questions

Discussion in 'Ruby' started by Barr, Keith, Aug 25, 2007.

  1. Barr, Keith

    Barr, Keith Guest

    --Boundary_(ID_cjdMwiw/0WtJODjPmwiKBw)
    Content-type: text/plain; charset=iso-8859-1
    Content-transfer-encoding: 7BIT

    Hi there,

    I am new to using Ruby and I am writing my first real programs and I have something I was wonndering about:

    I have a menu that I would like users to respond to simply by entering a single character. I have tried both STDIN.getc and gets, but both require a carriage return. Is there a command I haven't found that will actively read STDIN to process a single character as soon as it is entered?

    Thanks in advance,
    Keith

    --Boundary_(ID_cjdMwiw/0WtJODjPmwiKBw)--
     
    Barr, Keith, Aug 25, 2007
    #1
    1. Advertising

  2. Barr, Keith

    Dan Zwell Guest

    Barr, Keith wrote:
    > Hi there,
    >
    > I am new to using Ruby and I am writing my first real programs and I have something I was wonndering about:
    >
    > I have a menu that I would like users to respond to simply by entering a single character. I have tried both STDIN.getc and gets, but both require a carriage return. Is there a command I haven't found that will actively read STDIN to process a single character as soon as it is entered?
    >
    > Thanks in advance,
    > Keith


    This is a common question. You need a library (gem) that provides this
    function. I have heard that highline, ncurses, and termios have this. In
    ncurses, it's called "getch", but I don't know about the others.

    Dan
     
    Dan Zwell, Aug 25, 2007
    #2
    1. Advertising

  3. On Aug 25, 2007, at 3:36 PM, Dan Zwell wrote:

    > Barr, Keith wrote:
    >> Hi there,
    >> I am new to using Ruby and I am writing my first real programs
    >> and I have something I was wonndering about:
    >> I have a menu that I would like users to respond to simply by
    >> entering a single character. I have tried both STDIN.getc and
    >> gets, but both require a carriage return. Is there a command I
    >> haven't found that will actively read STDIN to process a single
    >> character as soon as it is entered?
    >> Thanks in advance,
    >> Keith

    >
    > This is a common question. You need a library (gem) that provides
    > this function. I have heard that highline, ncurses, and termios
    > have this. In ncurses, it's called "getch", but I don't know about
    > the others.


    Here's how you do it with HighLine:

    http://blog.grayproductions.net/articles/i_just_want_one_character

    James Edward Gray II
     
    James Edward Gray II, Aug 25, 2007
    #3
  4. On Aug 25, 3:05 pm, "Barr, Keith" <> wrote:
    > Hi there,
    >
    > I am new to using Ruby and I am writing my first real
    > programs and I have something I was wonndering about:
    >
    > I have a menu that I would like users to respond to
    > simply by entering a single character. I have tried
    > both STDIN.getc and gets, but both require a carriage
    > return. Is there a command I haven't found that will
    > actively read STDIN to process a single character as
    > soon as it is entered?


    if RUBY_PLATFORM =~ /win32/
    require 'Win32API'
    Kbhit = Win32API.new("msvcrt", "_kbhit", [], 'I')
    Getch = Win32API.new("msvcrt", "_getch", [], 'I')
    def getkey
    sleep 0.01
    return nil if Kbhit.call.zero?
    c = Getch.call
    c = Getch.call + 256 if c.zero? || c == 0xE0
    c
    end
    else
    def getkey
    select( [$stdin], nil, nil, 0.01 ) ? c = $stdin.getc : c = nil
    end
    end

    5.times{
    begin end until key = getkey
    print key.chr
    }
     
    William James, Aug 25, 2007
    #4
  5. Hi,

    Am Sonntag, 26. Aug 2007, 07:00:06 +0900 schrieb William James:
    > On Aug 25, 3:05 pm, "Barr, Keith" <> wrote:
    > > I have a menu that I would like users to respond to
    > > simply by entering a single character.

    >=20
    > def getkey
    > select( [$stdin], nil, nil, 0.01 ) ? c =3D $stdin.getc : c =3D nil
    > end


    Why `c=3D=C2=B4? Why twice?

    It doesn't work here; it still waits for the enter key.

    I doubt whether there is any other way than using termios.

    Bertram


    --=20
    Bertram Scharpf
    Stuttgart, Deutschland/Germany
    http://www.bertram-scharpf.de
     
    Bertram Scharpf, Aug 26, 2007
    #5
  6. Barr, Keith

    Wai Tsang Guest

    Bertram Scharpf wrote:
    > Hi,
    >
    > Am Sonntag, 26. Aug 2007, 07:00:06 +0900 schrieb William James:
    >> On Aug 25, 3:05 pm, "Barr, Keith" <> wrote:
    >> > I have a menu that I would like users to respond to
    >> > simply by entering a single character.

    >>
    >> def getkey
    >> select( [$stdin], nil, nil, 0.01 ) ? c = $stdin.getc : c = nil
    >> end

    >
    > Why `c=´? Why twice?
    >
    > It doesn't work here; it still waits for the enter key.
    >
    > I doubt whether there is any other way than using termios.
    >
    > Bertram


    It means if select( [$stdin], nil, nil, 0.01 ) return true,
    then c = $stdin.getc; otherwise c = nil.
    --
    Posted via http://www.ruby-forum.com/.
     
    Wai Tsang, Aug 26, 2007
    #6
  7. On Aug 25, 7:16 pm, Bertram Scharpf <> wrote:
    > Hi,
    >
    > Am Sonntag, 26. Aug 2007, 07:00:06 +0900 schrieb William James:
    >
    > > On Aug 25, 3:05 pm, "Barr, Keith" <> wrote:
    > > > I have a menu that I would like users to respond to
    > > > simply by entering a single character.

    >
    > > def getkey
    > > select( [$stdin], nil, nil, 0.01 ) ? c = $stdin.getc : c = nil
    > > end

    >
    > Why `c=´? Why twice?


    Don't ask me; it's not my code.
    >
    > It doesn't work here; it still waits for the enter key.


    It works here under windoze; I can't test it under unix.

    >
    > I doubt whether there is any other way than using termios.
     
    William James, Aug 26, 2007
    #7
  8. Hi,

    Am Sonntag, 26. Aug 2007, 15:15:04 +0900 schrieb William James:
    > On Aug 25, 7:16 pm, Bertram Scharpf <> wrote:
    > > Am Sonntag, 26. Aug 2007, 07:00:06 +0900 schrieb William James:
    > > > def getkey
    > > > select( [$stdin], nil, nil, 0.01 ) ? c = $stdin.getc : c = nil
    > > > end

    > >
    > > It doesn't work here; it still waits for the enter key.

    >
    > It works here under windoze; I can't test it under unix.


    That was what I meant: You did not test it on a POSIX
    system.

    You wrote:

    if RUBY_PLATFORM =~ /win32/
    [...]
    else
    def getkey
    select( [$stdin], nil, nil, 0.01 ) ? c = $stdin.getc : c = nil
    end
    end

    As long as there are no parsing errors this alway works
    under Windows. I tested it under Linux and there, it doesn't
    work.

    Bertram

    --
    Bertram Scharpf
    Stuttgart, Deutschland/Germany
    http://www.bertram-scharpf.de
     
    Bertram Scharpf, Aug 26, 2007
    #8
  9. Hi,

    Am Sonntag, 26. Aug 2007, 10:05:07 +0900 schrieb Wai Tsang:
    > Bertram Scharpf wrote:
    > > Am Sonntag, 26. Aug 2007, 07:00:06 +0900 schrieb William James:
    > >> On Aug 25, 3:05 pm, "Barr, Keith" <> wrote:
    > >>=20
    > >> select( [$stdin], nil, nil, 0.01 ) ? c =3D $stdin.getc : c =3D nil

    > > Why `c=3D=C2=B4? Why twice?

    >=20
    > It means if select( [$stdin], nil, nil, 0.01 ) return true,
    > then c =3D $stdin.getc; otherwise c =3D nil.


    The variable c will not be used and the assignment is
    mentioned twice. Besides that it is questionable whether an
    assignment in a ?: expression will parse how the author
    intended.

    That the function will return `true' is just a lie.

    Bertram


    --=20
    Bertram Scharpf
    Stuttgart, Deutschland/Germany
    http://www.bertram-scharpf.de
     
    Bertram Scharpf, Aug 26, 2007
    #9
  10. Barr, Keith

    John Joyce Guest

    On Aug 26, 2007, at 9:32 AM, Bertram Scharpf wrote:

    > Hi,
    >
    > Am Sonntag, 26. Aug 2007, 10:05:07 +0900 schrieb Wai Tsang:
    >> Bertram Scharpf wrote:
    >>> Am Sonntag, 26. Aug 2007, 07:00:06 +0900 schrieb William James:
    >>>> On Aug 25, 3:05 pm, "Barr, Keith" <> wrote:
    >>>>
    >>>> select( [$stdin], nil, nil, 0.01 ) ? c =3D $stdin.getc : c =3D =

    nil
    >>> Why `c=3D=B4? Why twice?

    >>
    >> It means if select( [$stdin], nil, nil, 0.01 ) return true,
    >> then c =3D $stdin.getc; otherwise c =3D nil.

    >
    > The variable c will not be used and the assignment is
    > mentioned twice. Besides that it is questionable whether an
    > assignment in a ?: expression will parse how the author
    > intended.
    >
    > That the function will return `true' is just a lie.
    >
    > Bertram

    Bertram, no need for inflamatory statements. Calling something 'just =20
    a lie' implies that the person is maliciously trying to spread false =20
    information. It might be more appropriate to simply say that it is a =20
    mistake and explain why.
     
    John Joyce, Aug 26, 2007
    #10
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Ali Syed
    Replies:
    3
    Views:
    582
    Mark McIntyre
    Oct 13, 2004
  2. pkirk25
    Replies:
    50
    Views:
    1,164
    Bill Pursell
    Oct 4, 2006
  3. Replies:
    10
    Views:
    580
  4. JohnE

    newbie with newbie questions

    JohnE, Aug 17, 2009, in forum: ASP .Net
    Replies:
    3
    Views:
    531
    Gregory A. Beamer
    Aug 17, 2009
  5. Vijay
    Replies:
    2
    Views:
    658
    Öö Tiib
    May 7, 2010
Loading...

Share This Page