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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T04:13:11+00:00 2026-05-26T04:13:11+00:00

In ruby, you can do this: class Thing public def f1 puts f1 end

  • 0

In ruby, you can do this:

class Thing
  public
  def f1
    puts "f1"
  end

  private
  def f2
    puts "f2"
  end

  public
  def f3
    puts "f3"
  end

  private
  def f4
    puts "f4"
  end
end

where now f1 and f3 and public, f2 and f4 is private. What is going on internally that allows you to invoke a class method that then changes the method definition? How can I implement the same functionality (ostensibly to create my own java like annotations)

for example…

class Thing
  fun
  def f1
    puts "hey"
  end

  notfun
  def f2
    puts "hey"
  end
end

and fun and notfun would change the following function definitions.

Thanks

  • 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-26T04:13:12+00:00Added an answer on May 26, 2026 at 4:13 am

    You can sometimes shove Ruby into an espressso cup. Let’s see how.

    Here’s a module FunNotFun…

    module FunNotFun
    
      def fun
        @method_type = 'fun'
      end
    
      def notfun
        @method_type = 'not fun'
      end
    
      def method_added(id)
        return unless @method_type
        return if @bypass_method_added_hook
        orig_method = instance_method(id)
        @bypass_method_added_hook = true
        method_type = @method_type
        define_method(id) do |*args|
          orig_method.bind(self).call(*args).tap do
            puts "That was #{method_type}"
          end
        end
        @bypass_method_added_hook = false
      end
    
    end
    

    … that you can use to extend a class …

    class Thing
    
      extend FunNotFun
    
      fun
      def f1
        puts "hey"
      end
    
      notfun
      def f2
        puts "hey"
      end
    end
    

    … with this result:

    Thing.new.f1
    # => hey
    # => That was fun
    
    Thing.new.f2
    # => hey
    # => That was not fun
    

    But see below the line for a better way.


    Annotations (see normalocity’s answer) are less trouble and, being a common Ruby idiom, will more easily communicate your code’s intent. Here’s how to do it with annotations:

    module FunNotFun
    
      def fun(method_id)
        wrap_method(method_id, "fun")
      end
    
      def notfun(method_id)
        wrap_method(method_id, "not fun")
      end
    
      def wrap_method(method_id, type_of_method)
        orig_method = instance_method(method_id)
        define_method(method_id) do |*args|
          orig_method.bind(self).call(*args).tap do
            puts "That was #{type_of_method}"
          end
        end
      end
    
    end
    

    In use, the annotation comes after the method is defined, rather than before:

    class Thing
    
      extend FunNotFun
    
      def f1
        puts "hey"
      end
      fun :f1
    
      def f2
        puts "hey"
      end
      notfun :f2
    
    end
    

    The result is the same:

    Thing.new.f1
    # => hey
    # => That was fun
    
    Thing.new.f2
    # => hey
    # => That was not fun
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

[ruby 1.8] Assume I have: dummy string do puts thing end Now, this is
is there any way I can reproduce this ruby function: def Password.hash(password,salt) Digest::SHA512.hexdigest(#{password}:#{salt}) end
How can I do this on Ruby? puts some_method(ò) # => ò In other
How can this line in Java be translated to Ruby: String className = java.util.Vector;
How can I do the Ruby method Flatten Ruby Method in C#. This method
This is a Ruby design problem. How can I make a reusable flat file
I can't seem to compile ironruby in ruby 1.8.7. I always get this error:
On windows, I can run my ruby script like this: > ruby myscript.rb but
I'm not sure this is possible, but in ruby, you can dynamically call a
Ruby can add methods to the Number class and other core types to get

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.