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

Chris Angelico rosuav at gmail.com
Wed Mar 28 12:04:26 EDT 2018


On Thu, Mar 29, 2018 at 12:28 AM, Steven D'Aprano
<steve+comp.lang.python at pearwood.info> wrote:
> On Tue, 27 Mar 2018 11:34:17 +1100, Chris Angelico wrote:
>
>> Question: How do you get a reference to a Ruby function? Or are they not
>> first-class objects?
>
> https://stackoverflow.com/questions/4294485/how-do-i-reference-a-function-
> in-ruby
>
> Especially this answer, which is worth reading:
>
> https://stackoverflow.com/a/4294660
>
> As best I can interpret it, Ruby starts with the premise that functions
> and methods are NOT first-class values -- or rather, since Ruby functions
> are actually methods of Object, there are no functions, only methods, and
> they are not first-class values.
>
> obj.method, despite superficially looking like the same as Ruby's dot
> syntax for attribute access, actually is syntax for calling the method.
>
> In practice, that's not as restrictive as it may seem. Where Python would
> pass a function object to a higher-order function, Ruby would usually
> pass an anonymous block or possibly a Proc.
>

Fair enough. There's no particular reason for *functions* per se to be
first-class, as long as you can do the same conceptual thing of "here,
you over there, have this piece of code to use". IMO
functions/callables are the cleanest way to do that, but if you have
to pass a code block, that's no worse than I've seen people do:
constructing an anonymous function that just calls some other
function. Not a big deal in the scheme of things.

ChrisA



More information about the Python-list mailing list