Project

General

Profile

Actions

Feature #12125

open

Proposal: Shorthand operator for Object#method

Added by Papierkorb (Stefan Merettig) about 8 years ago. Updated about 2 years ago.

Status:
Open
Assignee:
-
Target version:
-
[ruby-core:74036]

Description

Hello,

The & operator lets one pass a #call-able object as block.

Really useful feature, but at the moment, if you want to pass a Method this way the syntax is not really concise:

Dir["*/*.c"].map(&File.method(:basename))

More often than not, at least I end up writing this instead .map{|a| File.basename a} which isn't that great either.

Thus, I want to propose adding a short-hand operator to the ruby language, which simply calls #method on an Object.

It could look like this: an_object->the_method which is 100% equivalent to doing an_object.method(:the_method)

I'm reusing the -> operator which is already used for the stabby lambda. But I think it makes sense: You have an object,
and from that object you point at a method to get it as Method.

With this, the example from above becomes: Dir["*/*.c"].map(&File->basename)

I attached a proof of concept patch. When you apply this to trunk, you can try the example above yourself.
Do note however that this PoC also breaks stabby lambda for the moment. I'll work on fixing that the following
days.

Thank you for reading,
Stefan.


Files

method_shorthand.diff (740 Bytes) method_shorthand.diff Papierkorb (Stefan Merettig), 02/28/2016 09:36 PM
dot-symbol.patch (554 Bytes) dot-symbol.patch mame (Yusuke Endoh), 03/14/2016 04:43 PM

Related issues 2 (0 open2 closed)

Related to Ruby master - Feature #16275: Revert `.:` syntaxClosedActions
Has duplicate Ruby master - Feature #13581: Syntax sugar for method referenceClosedActions
Actions

Also available in: Atom PDF

Like0
Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0