From: Tudor Lupei on
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/.

From: brabuhr on
On Wed, Mar 31, 2010 at 2:47 PM, Tudor Lupei <tudor.lupei(a)gmail.com> wrote:
> 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.

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]

From: Tudor Lupei on
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/.

From: Tudor Lupei on
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/.

From: brabuhr on
On Wed, Mar 31, 2010 at 3:43 PM, Tudor Lupei <tudor.lupei(a)gmail.com> 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>