views:

40

answers:

1

Hi,

I am trying out Mongrel and using the following code:

require 'rubygems'
require 'mongrel'

class SimpleHandler < Mongrel::HttpHandler
    def process(request, response)
        response.start(200) do |head, out|
            head["Content-Type"] = "text/plain"
            out.write("Hello World!\n")
        end
    end
end

h = Mongrel::HttpServer.new("0.0.0.0", "3000")
h.register("/test", SimpleHandler.new)
puts "Press Control-C to exit"
h.run.join

trap("INT") do
    puts "Exiting..."
end

Basically, this just prints out "Hello World!" when I go to localhost:3000/test. It works fine, and I can close the program with Control-C. But when I press Control-C, this gets outputted:

my_web_server.rb:17:in `join': Interrupt
from my_web_server.rb:17

So I tried putting that trap("INT") statement at the end, but it isn't getting called. Solution?

Thanks.

+2  A: 

There's no need to trap INT if all you want to do is exit without the stack trace. A control-c causes an "Interrupt" exception. So to let your program exit on control-C without the ugly stack trace, catch that exception:

begin
  ... # do stuff
rescue Interrupt
  puts "Exiting..."
end
Wayne Conrad