Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8735429
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T10:03:24+00:00 2026-06-13T10:03:24+00:00

In Ruby, I’m well aware that the most recently registered signal trapper is the

  • 0

In Ruby, I’m well aware that the most recently registered signal trapper is the one that takes precedence, so:

Signal.trap("TERM") {puts "foo"; exit}
Signal.trap("TERM") {puts "bar"; exit}

If I were to issue a ‘kill’ to the above script, it would print out “bar” and exit.

Is there a way, in Ruby itself, to enforce some sort of scope for trappers?

My specific situation is this:

We have an application that defines its own trapper (for this story, we’ll just say it only traps “TERM”) in a namespaced Signals module. That code is required into the app at runtime and pretty much left alone after that.

A 3rd-party gem (I won’t name names) we use, actually registers its own trapper for “TERM” in the initialize method, so the most recently initialized object will be the one to handle the “TERM” signal for the entire application with undesirable results…

While it may make sense for the 3rd party gems’ objects to have their own trappers defined, I don’t want them affecting my app.

Is anyone aware of a way to give the non-initialize-non-3rd-party trapper precedence without getting too fancy?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T10:03:25+00:00Added an answer on June 13, 2026 at 10:03 am

    You could monkey patch Signal to alias Signal.trap and then replace it with a no-op:

    module Signal
        class << self
            alias_method :real_trap, :trap
            def trap(*) end
        end
    end
    

    Then you can use real_trap in your code and this rude unnamed gem you’re using will be ignored. You’d have to make sure Signal was patched before your rude-gem did anything though.

    With that patch in place, sending a SIGTERM to this code:

    Signal.trap("TERM") {puts "foo"; exit}
    Signal.trap("TERM") {puts "bar"; exit}
    Signal.real_trap("TERM") {puts "pancakes"; exit}
    

    would give you pancakes.

    I generally try to avoid monkey patching but it is quite useful for kludging around bugs and other bad behavior.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Ruby has a select method that takes an array and returns a subarray consisting
Ruby Newbie here, I understand that everything is an object in Ruby, one thing
Ruby on Rails question - where is the first point in rails code that
Ruby Motion comes with a lot of pre-built functions that are formatted like this:
Ruby is slow at certain things. But what parts of it are the most
Ruby 1.8.7, Rails 2.3.11 I've been trying the answers that appear to be what
Ruby setters—whether created by (c)attr_accessor or manually—seem to be the only methods that need
Ruby on Rails does not do multithreaded request-responses very well, or at least, ActiveRecord
Ruby on Rails has magic timestamping fields that are automatically updated when a record
ruby 1.8.7 does not compile well under Lion and Xcode 4.2. I'd like to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.