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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T07:31:07+00:00 2026-05-12T07:31:07+00:00

EDIT : I slightly changed the spec, to better match what I imagined this

  • 0

EDIT: I slightly changed the spec, to better match what I imagined this to do.

Well, I don’t really want to fake C# attributes, I want to one-up-them and support AOP as well.

Given the program:

class Object
  def Object.profile 
    # magic code here
  end
end 

class Foo
  # This is the fake attribute, it profiles a single method.
  profile
  def bar(b)
   puts b
  end

  def barbar(b)
    puts(b)
  end

  comment("this really should be fixed")
  def snafu(b)
  end

end

Foo.new.bar("test")
Foo.new.barbar("test") 
puts Foo.get_comment(:snafu)

Desired output:

Foo.bar was called with param: b = "test"
test
Foo.bar call finished, duration was 1ms
test
This really should be fixed

Is there any way to achieve this?

  • 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-12T07:31:08+00:00Added an answer on May 12, 2026 at 7:31 am

    I have a somewhat different approach:

    class Object
      def self.profile(method_name)
        return_value = nil
        time = Benchmark.measure do
          return_value = yield
        end
    
        puts "#{method_name} finished in #{time.real}"
        return_value
      end
    end
    
    require "benchmark"
    
    module Profiler
      def method_added(name)
        profile_method(name) if @method_profiled
        super
      end
    
      def profile_method(method_name)
        @method_profiled = nil
        alias_method "unprofiled_#{method_name}", method_name
        class_eval <<-ruby_eval
          def #{method_name}(*args, &blk)
            name = "\#{self.class}##{method_name}"
            msg = "\#{name} was called with \#{args.inspect}"
            msg << " and a block" if block_given?
            puts msg
    
            Object.profile(name) { unprofiled_#{method_name}(*args, &blk) }
          end
        ruby_eval
      end
    
      def profile
        @method_profiled = true
      end
    end
    
    module Comment
      def method_added(name)
        comment_method(name) if @method_commented
        super
      end
    
      def comment_method(method_name)
        comment = @method_commented
        @method_commented = nil
        alias_method "uncommented_#{method_name}", method_name
        class_eval <<-ruby_eval
          def #{method_name}(*args, &blk)
            puts #{comment.inspect}
            uncommented_#{method_name}(*args, &blk)
          end
        ruby_eval
      end
    
      def comment(text)
        @method_commented = text
      end
    end
    
    class Foo
      extend Profiler
      extend Comment
    
      # This is the fake attribute, it profiles a single method.
      profile
      def bar(b)
       puts b
      end
    
      def barbar(b)
        puts(b)
      end
    
      comment("this really should be fixed")
      def snafu(b)
      end
    end
    

    A few points about this solution:

    • I provided the additional methods via modules which could be extended into new classes as needed. This avoids polluting the global namespace for all modules.
    • I avoided using alias_method, since module includes allow AOP-style extensions (in this case, for method_added) without the need for aliasing.
    • I chose to use class_eval rather than define_method to define the new method in order to be able to support methods that take blocks. This also necessitated the use of alias_method.
    • Because I chose to support blocks, I also added a bit of text to the output in case the method takes a block.
    • There are ways to get the actual parameter names, which would be closer to your original output, but they don’t really fit in a response here. You can check out merb-action-args, where we wrote some code that required getting the actual parameter names. It works in JRuby, Ruby 1.8.x, Ruby 1.9.1 (with a gem), and Ruby 1.9 trunk (natively).
    • The basic technique here is to store a class instance variable when profile or comment is called, which is then applied when a method is added. As in the previous solution, the method_added hook is used to track when the new method is added, but instead of removing the hook each time, the hook checks for an instance variable. The instance variable is removed after the AOP is applied, so it only applies once. If this same technique was used multiple time, it could be further abstracted.
    • In general, I tried to stick as close to your “spec” as possible, which is why I included the Object.profile snippet instead of implementing it inline.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

EDIT, Changed the code slightly based on answers below, but still haven't got it
Edit one: Changes made based on Joseph's answer: In bytesToDrawable(byte[] imageBytes): Changed the following
EDIT 07/14 As Bill Burgess mentionned in a comment of his answer, this question
EDIT : It turned out that this can only be done through an external
EDIT: Simple version of the question: I want to create server variables in the
I know this is something I'm probably doing wrong, so please don't incinerate me
Edit: I'd like to plead temporary insanity for even asking this question, but it
So I'm having trouble understanding why one XPath expression gets the nodes I want,
I've just come across some code that's confusing me slightly; there are really 2
I use a mySQL query like this one to get some of the information

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.