Project

General

Profile

Actions

Feature #16273

open

Proposal: Shorthand operator for "#instance_method"

Added by osyo (manga osyo) about 5 years ago. Updated almost 5 years ago.

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

Description

hi, created issues to discuss shorthand for "#instance_method"

Overview

Ruby 2.7 adds a #method shorthand .: operator.

In this issues want to discuss the shorthand operator for "#instance_method".

Background

If you want to pass an array to Array#zip or Hash#merge as shown below, the code will be messy.

arrays = [["a", "b"], ["c"], ["d", "e"]]
hashs  = [{"a" => 1}, {"b" => 2, "c" => 3}, {"d" => 4, "e" => 5}]

# `#first` must be a receiver and the value excluding `#first` must be passed as an argument
arrays.first.zip(*arrays.drop(1))
arrays.first.product(*arrays.drop(1))
hashs.first.merge(*hashs.drop(1))

This can be solved by using # bind_call ([# 15955] (https://bugs.ruby-lang.org/issues/15955)).

arrays = [["a", "b"], ["c"], ["d", "e"]]
hashs  = [{"a" => 1}, {"b" => 2, "c" => 3}, {"d" => 4, "e" => 5}]

Array.instance_method(:zip).bind_call(*arrays)
Array.instance_method(:product).bind_call(*arrays)
Hash.instance_method(:merge).bind_call(*hashs)

But #instance_method is long.
I'm thinking shorthand operator for #instance_method.

:MEMO: There was a suggestion to add Array.zip or Array.product in the past

Proposal new operator

I can't come up with a suitable operator yet, sorry...
However, considering the .: operator, I think "operator + Symbol (:)" is good.

# Document-like writing
# `#` + operator
Array#zip
Array.#zip
Array#:zip

# Constant-link writing
# :: + :hoge
Array:::zip
Array::#zip

Also, the following syntax is valid at this time, so it may be difficult to adopt.

Array!zip
Array@zip
Array&zip
Array:zip

Please comment if you are interested in the shorthand operator for # instance_method.

  • Other #instance_method usecase
  • Proposal shorthand operator
  • etc...

Thank you! :)

Updated by nobu (Nobuyoshi Nakada) about 5 years ago

I don't think using # acceptable, and have thought .::.

Updated by naruse (Yui NARUSE) about 5 years ago

I don't think Array.instance_method(:zip).bind_call(*arrays) should be recommended, and worth adding shorthand operator.

Updated by mrkn (Kenta Murata) about 5 years ago

I don't think the first examples are messy.
On the contrary, the second examples using bind_call are quite messy than the first.

Updated by zverok (Victor Shepelev) about 5 years ago

Funnily enough (and not completely intentionally) the problem is "solved" with #16264:

.:zip.call(*arrays)
# or even... for those who likes to cry "code golf!"
.:zip.(*arrays)

Currently, you also can

:zip.to_proc.call(*arrays)

Though, I tend to agree with @mrkn (Kenta Murata) that "no-tricks" first example looks pretty clear... But I understand that in codebase where this metaphor emerges regularly, it could become tiresome and too non-atomic to read.

One another non-orthodox suggestion (which uses #then loathed by some, but of all the rest of examples the only one which reads directly "zip first array with the rest of them"):

arrays.then { |first, *rest| first.zip(*rest) }
Actions #5

Updated by znz (Kazuhiro NISHIYAMA) almost 5 years ago

  • Status changed from Open to Closed

Applied in changeset git|5aa0e6bee916f454ecf886252e1b025d824f7bd8.


Mention new feature of Hash#transform_keys [Feature #16273]

ref b25e27277dc39f25cfca4db8452d254f6cc8046e

Actions #6

Updated by znz (Kazuhiro NISHIYAMA) almost 5 years ago

  • Status changed from Closed to Open
Actions

Also available in: Atom PDF

Like0
Like0Like0Like0Like0Like0Like0