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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T08:39:32+00:00 2026-05-14T08:39:32+00:00

for instance in python it is possible to assign a method to a variable:

  • 0

for instance in python it is possible to assign a method to a variable:

class MyClass 
    def myMethod(self):
        return "Hi"


 x = MyClass()
 method = x.myMethod

 print method() # prints Hi

I know this should be possible in Ruby, but I don’t know what’s the syntax.

  • 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-14T08:39:33+00:00Added an answer on May 14, 2026 at 8:39 am

    You need to grab the method by using method with the method’s name as an argument. This will return you an instance of type Method, which can be called with call().

    class MyClass
      def myMethod
        "Hi"
      end
    end
    
    x = MyClass.new
    m = x.method(:myMethod)
    # => #<Method: MyClass#myMethod>
    
    puts m.call
    # You can also do m[] instead of m.call()
    

    Note that any arguments would need to be added to the call method.


    In many practical cases, however, there is no need to have the method itself saved to a variable in Ruby; if you just want to dynamically call a method (i.e. send a message to an object) and there is no need to save the method, you could also use the send (or __send__ method in case of name clashes).

    x = MyClass.new
    puts x.send :myMethod # also possible with a string: m.send "myMethod"
    # "Hi"
    

    Any arguments should follow the method name:

    puts x.send(:myMethod, arg1, arg2)
    

    To use it like this is probably more Ruby-like, as the concept of Method classes is not as prominent as it is in Python. In Python, you can always think of a two step mechanism when doing something like a_string.split(); first you grab the method with a_string.split and then you call it (either implicitly with () or explicitly with __call__()). So, cutting that two-step mechanism is rather natural to do.

    Ruby is more based on message passing and to actually get a method class in Ruby, you’ll have to do some more work, because in some way, the method object will have to be constructed for you at that point. So, unless you really need some Methods object in Ruby, you should rather stick to the message passing abstraction and simply use send.

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

Sidebar

Related Questions

Lets assume we have a class in python: class A(object): def __del__(self): print Del!
Possible Duplicate: Instance variables vs. class variables in Python What is the difference between
Is it possible to remove a class definition from a running instance of Python?
Possible Duplicate: Assign a value to class variable is assiging it for all instances
Is there a way to transfer a new class instance (python class that inherits
Is there no magic python way of accessing the instance of the class that
I'm having trouble creating an instance of a class using Python's re module. Here's
I've been trying to understand Python's handling of class and instance variables. In particular,
Is it possible to make 3rd party external executable to use instance of python
This is a combination of my two recent questions: [1] Python instance method in

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.