Proper way to allow Control-C to interrupt IO#gets

Discussion in 'Ruby' started by Lloyd Zusman, Feb 12, 2008.

  1. Lloyd Zusman

    Lloyd Zusman Guest

    What is the canonical method for allowing an IO#gets call to be
    immediately interrupted by Control-C? This doesn't seem to work as I am
    expecting. Consider the following bit of ruby code:

    trap("INT") do
    puts "got signal INT"
    end
    puts "Enter some characters followed by newline:"
    result = gets
    puts result

    When I run this and enter a few characters interspersed with an instance
    of Control-C, the Control-C doesn't get recognized until after I hit
    newline. Assuming that this program is in a file called "cctest",
    here's what happens:

    % ruby cctest
    Enter some characters followed by newline:
    [ here I enter AB^CDEFG, where "^C" is Control-C, which
    doesn't cause the input to be aborted ]
    ABDEFG <- This is what echos when I'm typing.
    got signal INT <- This is what echos after I hit newline,
    DEFG <- ... and this, as well.

    In other words, the SIGINT resulting from Control-C doesn't get
    processed at until after I type the newline.

    I've searched for discussions of this on the net, but I haven't found
    anything under a "ruby interrupt gets" Google search, because the word
    "gets" is used so commonly in English in so many other contexts.

    I know that I can write my own input handler or use something like the
    HighLine package, but I'm wondering whether there's a way to cause
    IO#gets to do what I want.

    By the way, in case this is pertinent ...

    % ruby --version
    ruby 1.8.5 (2006-12-25 patchlevel 12) [i686-linux]

    % uname -mor
    2.6.9-022stab078.20-enterprise i686 GNU/Linux

    Thanks in advance.


    --
    Lloyd Zusman

    God bless you.
     
    Lloyd Zusman, Feb 12, 2008
    #1
    1. Advertising

  2. Lloyd Zusman

    Lloyd Zusman Guest

    I left out a "Kernel.exit(0)" call in my test, but I still have the same
    problemL

    trap("INT") do
    puts "got signal INT"
    Kernel.exit(0)
    end
    puts "Enter some characters followed by newline:"
    result = gets
    puts result

    % ruby cctest
    Enter some characters followed by newline:
    [ here I enter AB^CDEFG, where "^C" is Control-C, which
    doesn't cause the input to be aborted ]
    ABDEFG <- This is what echos when I'm typing.
    got signal INT <- This is what echos after I hit newline.

    In other words, the SIGINT still doesn't get processed until
    after the newline is entered.


    Lloyd Zusman <> writes:

    > What is the canonical method for allowing an IO#gets call to be
    > immediately interrupted by Control-C? This doesn't seem to work as I am
    > expecting. Consider the following bit of ruby code:
    >
    > trap("INT") do
    > puts "got signal INT"
    > end
    > puts "Enter some characters followed by newline:"
    > result = gets
    > puts result
    >
    > When I run this and enter a few characters interspersed with an instance
    > of Control-C, the Control-C doesn't get recognized until after I hit
    > newline. Assuming that this program is in a file called "cctest",
    > here's what happens:
    >
    > % ruby cctest
    > Enter some characters followed by newline:
    > [ here I enter AB^CDEFG, where "^C" is Control-C, which
    > doesn't cause the input to be aborted ]
    > ABDEFG <- This is what echos when I'm typing.
    > got signal INT <- This is what echos after I hit newline,
    > DEFG <- ... and this, as well.
    >
    > In other words, the SIGINT resulting from Control-C doesn't get
    > processed at until after I type the newline.
    >
    > I've searched for discussions of this on the net, but I haven't found
    > anything under a "ruby interrupt gets" Google search, because the word
    > "gets" is used so commonly in English in so many other contexts.
    >
    > I know that I can write my own input handler or use something like the
    > HighLine package, but I'm wondering whether there's a way to cause
    > IO#gets to do what I want.
    >
    > By the way, in case this is pertinent ...
    >
    > % ruby --version
    > ruby 1.8.5 (2006-12-25 patchlevel 12) [i686-linux]
    >
    > % uname -mor
    > 2.6.9-022stab078.20-enterprise i686 GNU/Linux
    >
    > Thanks in advance.
    >
    >
    > --
    > Lloyd Zusman
    >
    > God bless you.
    >
    >
    >


    --
    Lloyd Zusman

    God bless you.
     
    Lloyd Zusman, Feb 12, 2008
    #2
    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. Stefan Mueller
    Replies:
    5
    Views:
    534
    Steven Saunderson
    Jul 10, 2006
  2. Jeff
    Replies:
    2
    Views:
    971
    clintonG
    Sep 19, 2006
  3. Ryan Taylor
    Replies:
    1
    Views:
    700
    Ryan Taylor
    Sep 9, 2004
  4. John Joyce

    gets gets

    John Joyce, Mar 26, 2007, in forum: Ruby
    Replies:
    2
    Views:
    362
    John Joyce
    Mar 26, 2007
  5. John Joyce

    Return of gets gets

    John Joyce, Apr 23, 2007, in forum: Ruby
    Replies:
    0
    Views:
    201
    John Joyce
    Apr 23, 2007
Loading...

Share This Page