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

T

Tudor Lupei

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
 
B

brabuhr

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]
 
T

Tudor Lupei

unknown said:
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>
 
T

Tudor Lupei

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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top