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 588785
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T15:21:19+00:00 2026-05-13T15:21:19+00:00

I’ve inherited a large pile of Ruby code that’s, frankly, close to impossible to

  • 0

I’ve inherited a large pile of Ruby code that’s, frankly, close to impossible to understand for a mortal like myself. It’s actually Rspec unit test code, but the structure is “highly unusual” to put it nicely.

What I’d like to be able to do is run the code, and have the following information logged somewhere:

  • every method that gets invoked, including the name of the class that defines the method, and the filename where the invoked method has been defined (yep, we’ve got the same class/method defined in multiple different files, and it’s tough to know which is being invoked)
  • (optionally) the parameters passed to each method invoked

With that, I could start trying to refactor it. Without it, it’s going to be a very difficult task to get it straightened out, due to the size of the code base (20k+ unit test cases).

I can’t afford to go in and perform wholesale edits to the code being run, because it breaks when you even use harsh language around it (i.e. frequently). Instead, I need to be able to instrument the code in its existing state, or with minimal changes to what exists now.

Is there a way of logging this level of detail without making wholesale changes to the code base? I’ve had a look at the Ruby profiler to see if it could help, and it probably could; I’m curious if there’s a better way (particularly logging the filename containing the invoked method).

Thanks in advance

  • 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-05-13T15:21:19+00:00Added an answer on May 13, 2026 at 3:21 pm

    This is definitely possible — in fact, there’s even a method for it! Just add this somewhere in your code before the point that you want to start logging things:

    set_trace_func proc { |event, file, line, id, binding, classname|
      printf "%8s %s:%-2d %10s %8s\n", event, file, line, id, classname
    }
    

    The secret sauce you want comes from Kernel#set_trace_func, as noted above:

    • set_trace_func(proc) => proc
    • set_trace_func(nil) => nil

    Establishes proc as the handler for tracing, or disables tracing if the parameter is nil. proc takes up to six parameters: an event name, a filename, a line number, an object id, a binding, and the name of a class. proc is invoked whenever an event occurs. Events are: c-call (call a C-language routine), c-return (return from a C-language routine), call (call a Ruby method), class (start a class or module definition), end (finish a class or module definition), line (execute code on a new line), raise (raise an exception), and return (return from a Ruby method). Tracing is disabled within the context of proc.

    Here’s a handy example:

    class Test
      def test
        a = 1
        b = 2
      end
    end
    
    set_trace_func proc { |event, file, line, id, binding, classname|
      printf "%8s %s:%-2d %10s %8s\n", event, file, line, id, classname
    }
    
    t = Test.new
    t.test
    

    (Note: don’t try this in irb unless you want a huge scrolling screen of text.) The resulting output is:

        line test.rb:11               false
      c-call test.rb:11        new    Class
      c-call test.rb:11 initialize   Object
    c-return test.rb:11 initialize   Object
    c-return test.rb:11        new    Class
        line test.rb:12               false
        call test.rb:2        test     Test
        line test.rb:3        test     Test
        line test.rb:4        test     Test
      return test.rb:4        test     Test
    

    You can play around with the formatting string above to get just the results you want to log (for example, it sounds like you’re only interested in call events).

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

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I've got a string that has curly quotes in it. I'd like to replace
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I would like to count the length of a string with PHP. The string
For some reason, after submitting a string like this Jack’s Spindle from a text
I am trying to understand how to use SyndicationItem to display feed which is
I have this code to decode numeric html entities to the UTF8 equivalent character.
I have a French site that I want to parse, but am running into

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.