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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T07:29:42+00:00 2026-05-18T07:29:42+00:00

In python, it’s fairly straightforward to reference a function: >>> def foo(): … print

  • 0

In python, it’s fairly straightforward to reference a function:

>>> def foo():
...     print "foo called"
...     return 1
... 
>>> x = foo
>>> foo()
foo called
1
>>> x()
foo called
1
>>> x
<function foo at 0x1004ba5f0>
>>> foo
<function foo at 0x1004ba5f0>

However, it seems to be different in Ruby since a naked foo actually calls foo:

ruby-1.9.2-p0 > def foo
ruby-1.9.2-p0 ?>  print "foo called"
ruby-1.9.2-p0 ?>  1
ruby-1.9.2-p0 ?>  end
 => nil 
ruby-1.9.2-p0 > x = foo
foo called => 1 
ruby-1.9.2-p0 > foo
foo called => 1 
ruby-1.9.2-p0 > x
 => 1 

How do I actually assign the function foo to x and then call it? Or is there a more idiomatic way to do 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-18T07:29:43+00:00Added an answer on May 18, 2026 at 7:29 am

    Ruby doesn’t have functions. It only has methods (which aren’t first-class) and Procs which are first-class, but are not associated with any object.

    So, this is a method:

    def foo(bar) puts bar end
    
    foo('Hello')
    # Hello
    

    Oh, and, yes, this is a real method, not a top-level function or procedure or something. Methods defined at the top-level end up as private(!) instance methods in the Object class:

    Object.private_instance_methods(false) # => [:foo]
    

    This is a Proc:

    foo = -> bar { puts bar }
    
    foo.('Hello')
    # Hello
    

    Notice that Procs are called differently from methods:

    foo('Hello')  # method
    foo.('Hello') # Proc
    

    The foo.(bar) syntax is just syntactic sugar for foo.call(bar) (which for Procs and Methods is also aliased to foo[bar]). Implementing a call method on your object and then calling it with .() is the closest thing you will get to Python’s __call__ables.

    Note that an important distinction between Ruby Procs and Python lambdas is that there are no restrictions: in Python, a lambda can only contain a single statement, but Ruby doesn’t have the distinction between statements and expressions (everything is an expression), and so this limitation simply doesn’t exist, therefore in a lot of cases where you need to pass a named function as an argument in Python because you cannot express the logic in a single statement, you would in Ruby simply pass a Proc or a block instead, so that the problem of the ugly syntax for referencing methods doesn’t even arise.

    You can wrap a method in a Method object (which essentially duck-types Proc) by calling the Object#method method on an object (which will give you a Method whose self is bound to that particular object):

    foo_bound = method(:foo)
    
    foo_bound.('Hello')
    # Hello
    

    You can also use one of the methods in the Module#instance_method family to get an UnboundMethod from a module (or class, obviously, since a class is-a module), which you can then UnboundMethod#bind to a particular object and call. (I think Python has the same concepts, albeit with a different implementation: an unbound method simply takes the self argument explicitly, just like the way it is declared.)

    foo_unbound = Object.instance_method(:foo) # this is an UnboundMethod
    
    foo_unbound.('Hello')
    # NoMethodError: undefined method `call' for #<UnboundMethod: Object#foo>
    
    foo_rebound = foo_unbound.bind(self)       # this is a Method
    
    foo_rebound.('Hello')
    # Hello
    

    Note that you can only bind an UnboundMethod to an object which is an instance of the module you took the method from. You cannot use UnboundMethods to “transplant” behavior between unrelated modules:

    bar = module Foo; def bar; puts 'Bye' end; self end.instance_method(:bar)
    module Foo; def bar; puts 'Hello' end end
    
    obj = Object.new
    bar.bind(obj)
    # TypeError: bind argument must be an instance of Foo
    
    obj.extend(Foo)
    bar.bind(obj).()
    # Bye
    obj.bar
    # Hello
    

    Note, however, that both the Method and the UnboundMethod are wrappers around the method, not the method itself. Methods are not objects in Ruby. (Contrary to what I have written in other answers, BTW. I really need to go back and fix those.) You can wrap them in objects, but they aren’t objects, and you can see that because you essentially get all the same problems you always get with wrappers: identity and state. If you call method multiple times for the same method, you will get a different Method object every time. If you try to store some state on that Method object (such as Python-style __doc__strings, for example), that state will be private to that particular instance, and if you try to retrieve your docstring again via method, you will find that it is gone.

    There is also syntactic sugar in the form of the method reference operator .::

    bound_method = obj.:foo
    

    Which is identical to

    bound_method = obj.method(:foo)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Python 2.5 to 2.7: #a.py: def foo(): pass #b.py from a import foo foo()
[Python 3.1] I'm following up on this answer : class prettyfloat(float): def __repr__(self): return
Python uses the reference count method to handle object life time. So an object
python's time module seems a little haphazard. For example, here is a list of
Python's IDLE has 'Check Module' (Alt-X) to check the syntax which can be called
Python elementTree seems unusable with namespaces. What are my alternatives? BeautifulSoup is pretty rubbish
Python's fcnt module provides a method called [flock][1] to proved file locking. It's description
[Python 3.1] Edit: mistake in the original code. I need to print a table.
Python has this wonderful way of handling string substitutions using dictionaries: >>> 'The %(site)s
Python works on multiple platforms and can be used for desktop and web applications,

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.