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

  • Home
  • SEARCH
  • 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 931403
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T20:29:42+00:00 2026-05-15T20:29:42+00:00

I want to make a hook method which gets called everytime any function of

  • 0

I want to make a hook method which gets called everytime any function of a class gets called.
I have tried method_added, but it executes only once at the time of class definition,

class Base

  def self.method_added(name)
    p "#{name.to_s.capitalize} Method's been called!!"
  end
  def a
    p "a called."
  end
  def b
    p "b called."
  end
end
t1 = Base.new
t1.a
t1.b
t1.a
t1.b

Output:

"A Method's been called!!"
"B Method's been called!!"
"a called."
"b called."
"a called."
"b called."

but my requirement is that any function of a class that gets called anywhere in the program triggers the “method_called”, hook method.

Expected Output:
"A Method's been called!!"
"a called."
"B Method's been called!!"
"b called."
"A Method's been called!!"
"a called."
"B Method's been called!!"
"b called."

If there is any defined existing hook method that works just the same, then please tell about it.

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-15T20:29:43+00:00Added an answer on May 15, 2026 at 8:29 pm

    Take a look at Kernel#set_trace_func. It lets you specify a proc which is invoked whenever an event (such as a method call) occurs. Here’s an example:

    class Base
      def a
        puts "in method a"
      end
    
      def b
        puts "in method b"
      end
    end
    
    set_trace_func proc { |event, file, line, id, binding, classname|
      # only interested in events of type 'call' (Ruby method calls)
      # see the docs for set_trace_func for other supported event types
      puts "#{classname} #{id} called" if event == 'call'
    }
    
    b = Base.new
    b.a
    b.b
    

    Outputs:

    Base a called
    in method a
    Base b called
    in method b
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 495k
  • Answers 495k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer If the compilation element in your web.config file has the… May 16, 2026 at 11:14 am
  • Editorial Team
    Editorial Team added an answer You definitely can use IF's in a UDF. There are… May 16, 2026 at 11:14 am
  • Editorial Team
    Editorial Team added an answer Instead of a LinkedList just use an ArrayList - you… May 16, 2026 at 11:14 am

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Related Questions

I have a controller called articles , which creates the articles model which gets
I want to make somethink like hyperlink. Right now, i created button, which opens
I want to make a special calendar app, but I am afraid it's not
So I have a helper method that looks something like the following: private D
I'm using .NET 3.5. We have some complex third-party classes which are automatically generated
I have a LINQ partial class: public partial class resp { public IEnumerable<RuleViolation> GetRuleViolations()
is there any option to sendmail linux command on particular time . I want
i have the different image with the different size too, i want to put
I want to make a div that expands to reveal its content when clicked.
I want to make my dialogs non-modal, i.e.: When opening multiple dialogs, they could

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.