Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > Proper way to allow Control-C to interrupt IO#gets

Reply
Thread Tools

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

 
 
Lloyd Zusman
Guest
Posts: n/a
 
      02-12-2008
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
http://www.velocityreviews.com/forums/(E-Mail Removed)
God bless you.


 
Reply With Quote
 
 
 
 
Lloyd Zusman
Guest
Posts: n/a
 
      02-12-2008
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 <(E-Mail Removed)> 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
> (E-Mail Removed)
> God bless you.
>
>
>


--
Lloyd Zusman
(E-Mail Removed)
God bless you.


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
newbie: allow deny vs deny allow Jeff ASP .Net 2 09-19-2006 02:12 AM
Proper way to upgrade Thunderbird 1.0 to 1.0.2? Z Firefox 2 03-27-2005 01:04 AM
Location element in the Web.config file. Allow System Admin whole directory, allow others specific page Ryan Taylor ASP .Net Security 1 09-09-2004 06:52 PM
Proper way to install the Bird & the Fox? @micro$oft.linuxworx Firefox 0 04-25-2004 04:22 AM
Re: What is a proper way to set up smtp Steve C. Orr, MCSD ASP .Net 1 07-03-2003 11:23 AM



Advertisments