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

The Archive Base Latest Questions

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

I’m trying to add logging to a method from the outside (Aspect-oriented-style) class A

  • 0

I’m trying to add logging to a method from the outside (Aspect-oriented-style)

class A
  def test
    puts "I'm Doing something..."
  end
end

class A # with logging!
  alias_method :test_orig, :test
  def test
    puts "Log Message!"
    test_orig
  end
end

a = A.new
a.test

The above works alright, except that if I ever needed to do alias the method again, it goes into an infinite loop. I want something more like super, where I could extend it as many times as I needed, and each extension with alias its parent.

  • 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-13T12:19:14+00:00Added an answer on May 13, 2026 at 12:19 pm

    Another alternative is to use unbound methods:

    class A
      original_test = instance_method(:test)
      define_method(:test) do
        puts "Log Message!"
        original_test.bind(self).call
      end
    end
    
    class A
      original_test = instance_method(:test)
      counter = 0
      define_method(:test) do
        counter += 1
        puts "Counter = #{counter}"
        original_test.bind(self).call
      end
    end
    
    irb> A.new.test
    Counter = 1
    Log Message!
    #=> #....
    irb> A.new.test
    Counter = 2
    Log Message!
    #=> #.....
    

    This has the advantage that it doesn’t pollute the namespace with additional method names, and is fairly easily abstracted, if you want to make a class method add_logging or what have you.

    class Module
      def add_logging(*method_names)
        method_names.each do |method_name|
          original_method = instance_method(method_name)
          define_method(method_name) do |*args,&blk|
            puts "logging #{method_name}"
            original_method.bind(self).call(*args,&blk)
          end
        end
      end
    end
    
    class A
      add_logging :test
    end
    

    Or, if you wanted to be able to do a bunch of aspects w/o a lot of boiler plate, you could write a method that writes aspect-adding methods!

    class Module
      def self.define_aspect(aspect_name, &definition)
        define_method(:"add_#{aspect_name}") do |*method_names|
          method_names.each do |method_name|
            original_method = instance_method(method_name)
            define_method(method_name, &(definition[method_name, original_method]))
          end
        end
      end
      # make an add_logging method
      define_aspect :logging do |method_name, original_method|
        lambda do |*args, &blk|
          puts "Logging #{method_name}"
          original_method.bind(self).call(*args, &blk)
        end
      end
      # make an add_counting method
      global_counter = 0
      define_aspect :counting do |method_name, original_method|
         local_counter = 0
         lambda do |*args, &blk|
           global_counter += 1
           local_counter += 1
           puts "Counters: global@#{global_counter}, local@#{local_counter}"
           original_method.bind(self).call(*args, &blk)
         end
      end      
    end
    
    class A
      def test 
        puts "I'm Doing something..." 
      end
      def test1 
        puts "I'm Doing something once..." 
      end
      def test2
        puts "I'm Doing something twice..." 
        puts "I'm Doing something twice..." 
      end
      def test3
        puts "I'm Doing something thrice..." 
        puts "I'm Doing something thrice..." 
        puts "I'm Doing something thrice..." 
      end
      def other_tests
        puts "I'm Doing something else..." 
      end
    
      add_logging :test, :test2, :test3
      add_counting :other_tests, :test1, :test3
    end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

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

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

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

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

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer If you have a good reason to care about performance...… May 15, 2026 at 7:15 am
  • Editorial Team
    Editorial Team added an answer SelectedIndex is -1 if nothing's selected, right? Reverse your logic… May 15, 2026 at 7:15 am
  • Editorial Team
    Editorial Team added an answer I'm assuming you're using this to get around the IE6… May 15, 2026 at 7:15 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

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.