Ruby parens-free function calls [was Re: Accessing parent objects]

Rick Johnson rantingrickjohnson at gmail.com
Tue Mar 27 19:16:11 EDT 2018


On Tuesday, March 27, 2018 at 4:47:05 PM UTC-5, Gregory Ewing wrote:
> Rick Johnson wrote:
> >     rb> Object.method("print_name").call("Meathead")
>
> Yes, but the point is that you have to have to use a different
> syntax to call it. This is like having to say
>
>     f.__call__(arg)
>
> in Python instead of just
>
>     f(arg)

You _can_ call a Ruby func/method directly by name, and i've
already demonstrated that fact. For example, the following is
perfectly legit (although untested):

    def f(arg)
        nil
    end
    f("arg")

And here is the equivalent code in Python (notice the
similarities):

    def f(arg):
        pass
    f("arg")

The only difference is when you want to make a call from a
_reference_, which, as you and i well know, is not the most
common way func/meths are called (these are rare).

Here is Ruby 1.9:

    ref = Object.method("f")
    ref.call("arg")

And the Python equivalent:

    ref = f
    ref("arg")

    NOTE: Of course, the "Python equivalent" assumes `f` is in
    the current module space, whereas in Ruby, Object is
    available _everywhere_.

As you can see, there is nothing "magical" about the Ruby
code. Sure, one could claim it is more onerous (and i would
agree). But to claim it is "magic" (as Chris did) is absurd.

PS: I must stress again that i am using Ruby version 1.9 and
have been for many years. I must also stress there have been
many changes in the Ruby language since 1.9 and i have not
kept up with them. So, it may be possible to do `ref("arg")`
in the newer versions. The point is, before anyone accuses
Ruby of not doing X, Y or Z -- they really should go read
the docs.



More information about the Python-list mailing list