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

The Archive Base Latest Questions

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

I was wondering how to access a global function fn in ruby from a

  • 0

I was wondering how to access a global function fn in ruby from a class which also defined a method fn. I have made a workaround by aliasing the function like so:

def fn
end

class Bar
    alias global_fn fn
    def fn
        # how to access the global fn here without the alias
        global_fn
    end
end

I’m looking for something along the lines of c++’s :: to access global scope but I can’t seem to locate any information about it. I guess I don’t know specifically what I’m looking for.

  • 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-14T07:07:25+00:00Added an answer on May 14, 2026 at 7:07 am

    At the top-level a def adds a private method to Object.

    I can think of three ways to get the top-level function:

    (1) Use send to invoke the private method on Object itself (only works if the method is not a mutator since Object will be the receiver)

    Object.send(:fn)
    

    (2) Get a Method instance of the top-level method and bind it to the instance you want to invoke it on:

    class Bar
      def fn
        Object.instance_method(:fn).bind(self).call
      end
    end
    

    (3) Use super (assumes no super classes of Bar below Object redefine the function)

    class Bar
      def fn
        super
      end
    end
    

    UPDATE:

    Since solution (2) is the preferable one (in my opinion) we can try to improve the syntax by defining a utility method on Object called super_method:

    class Object
      def super_method(base, meth, *args, &block)
        if !self.kind_of?(base) 
          raise ArgumentError, "#{base} is not a superclass of #{self}" 
        end
    
        base.instance_method(meth).bind(self).call(*args, &block)
      end
    end
    

    Use like the following:

    class Bar
      def fn
        super_method Object, :fn
      end
    end
    

    Where the first argument to super_method must be a valid superclass of Bar, the second argument the method you want to invoke, and all remaining arguments (if any) are passed along as parameters to the selected method.

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

Sidebar

Related Questions

just was wondering if there is a way to access a method from my
I can access $conn from within my function like so: function xyz($a){ global $conn;
I'm wondering if it's possible to access page JavaScript variables with Selenium. I have
I'm wondering how to access parent class in my Qt application. Lets say my
I don't have access to global php.ini file since I am running my application
I'm wondering how to access the name variable with a function in a javascript
i am wondering could i access my app internal storage using emulator. I mean
Just wondering what the best way to access read only information form a Microsoft
Just wondering if it is possible to access the Mac OS X Address Book
I was wondering how I can access the keyboard list that you normally access

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.