trap(:INT); system(); STDIN.gets(); ^C

Discussion in 'Ruby' started by Tudor Lupei, Mar 31, 2010.

  1. Tudor Lupei

    Tudor Lupei Guest

    Considering a trivial example:


    trap :INT do
    puts
    puts 'Bye'
    exit
    end

    # system 'date'
    print 'Say something: '
    STDOUT.flush
    puts "You said: #{STDIN.gets}"


    When prompted to say something, hitting Ctrl+C will call trap:)INT)
    block.
    However, after uncommenting line 7, Ctrl+C won't cause a call to
    trap:)INT) block unless I send a newline or EOF.
    Can anyone shed some light on this one?

    Platform: linux; ruby 1.8.7

    Thanks
    --
    Posted via http://www.ruby-forum.com/.
     
    Tudor Lupei, Mar 31, 2010
    #1
    1. Advertising

  2. Tudor Lupei

    Guest

    On Wed, Mar 31, 2010 at 2:47 PM, Tudor Lupei <> wrote:
    > trap :INT do
    > =A0puts
    > =A0puts 'Bye'
    > =A0exit
    > end
    >
    > # system 'date'
    > print 'Say something: '
    > STDOUT.flush
    > puts "You said: #{STDIN.gets}"
    >
    >
    > When prompted to say something, hitting Ctrl+C will call trap:)INT)
    > block.
    > However, after uncommenting line 7, Ctrl+C won't cause a call to
    > trap:)INT) block unless I send a newline or EOF.


    With or without line 7, here both programs behave the same (Ctrl-C +
    EOF -> Bye):

    $ ruby a.rb
    Say something:
    Bye
    $ ruby b.rb
    Wed Mar 31 15:43:32 EDT 2010
    Say something:
    Bye

    > Platform: linux; ruby 1.8.7


    $ uname -a
    Linux mcu.claw.ctc.com 2.6.18-164.11.1.el5 #1 SMP Wed Jan 20 07:39:04
    EST 2010 i686 i686 i386 GNU/Linux
    $ ruby -v
    ruby 1.8.5 (2006-08-25) [i386-linux]
     
    , Mar 31, 2010
    #2
    1. Advertising

  3. Tudor Lupei

    Tudor Lupei Guest

    unknown wrote:
    > With or without line 7, here both programs behave the same (Ctrl-C +
    > EOF -> Bye):


    So you need to hit ^C^D every time?

    Here's the behavior I'm getting:

    # without system('date')
    $ ruby test.rb
    Say something: ^C
    Bye

    # with system('date')
    $ ruby test.rb
    Wed Mar 31 20:04:28 EEST 2010
    Say something: ^C <I can still type here>
    --
    Posted via http://www.ruby-forum.com/.
     
    Tudor Lupei, Mar 31, 2010
    #3
  4. Tudor Lupei

    Tudor Lupei Guest

    Might be worth to mention that both of the alternatives below will
    immediately exit after ^C


    ### ALT 1
    trap :INT do
    puts
    puts 'Bye'
    exit
    end

    puts IO.popen('date').gets
    print 'Say something: '
    STDOUT.flush
    puts "You said: #{STDIN.gets}"


    ### ALT 2
    begin
    system 'date'
    print 'Say something: '
    STDOUT.flush
    puts "You said: #{STDIN.gets}"
    rescue Interrupt
    puts
    puts 'Bye'
    exit
    end
    --
    Posted via http://www.ruby-forum.com/.
     
    Tudor Lupei, Mar 31, 2010
    #4
  5. Tudor Lupei

    Guest

    On Wed, Mar 31, 2010 at 3:43 PM, Tudor Lupei <> wrote:
    > unknown wrote:
    >> With or without line 7, here both programs behave the same (Ctrl-C +
    >> EOF -> Bye):

    >
    > So you need to hit ^C^D every time?


    yes, on this host both versions act like

    > # with system('date')
    > $ ruby test.rb
    > Wed Mar 31 20:04:28 EEST 2010
    > Say something: ^C <I can still type here>
     
    , Apr 1, 2010
    #5
    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. Schnoffos
    Replies:
    2
    Views:
    1,236
    Martien Verbruggen
    Jun 27, 2003
  2. Hal Styli
    Replies:
    14
    Views:
    1,681
    Old Wolf
    Jan 20, 2004
  3. arun
    Replies:
    8
    Views:
    467
    Dave Thompson
    Jul 31, 2006
  4. aling
    Replies:
    8
    Views:
    980
    Jim Langston
    Oct 20, 2005
  5. Replies:
    9
    Views:
    451
    James Kanze
    Apr 17, 2007
Loading...

Share This Page