Project

General

Profile

Actions

Feature #21857

open

Introduce to_proc pattern

Feature #21857: Introduce to_proc pattern

Added by sanfrecce-osaka (Masatoshi Moritsuka) 22 days ago. Updated 13 days ago.

Status:
Assigned
Target version:
-
[ruby-dev:<unknown>]

Description

When object has some logics, we can use &object syntax with Enumerable or Array, etc.

users.any?(&:admin?)

There are some times when I want to write pattern matching similar to the syntax, but currently we either have to write proc or combine pin operator with to_proc, which is a bit verbose.

users in [*, ^(:admin?.to_proc), *]
users in [*, -> (user) { user.admin? }, *]

Therefore, I propose introducing a new pattern into pattern matching.
The pattern matches an object such that pattern.to_proc === object.
This allows us to write code with a familiarity close to the syntax we're accustomed to.

users in [*, &:admin?, *]

Updated by matheusrich (Matheus Richard) 15 days ago Actions #1

Could you add why one would prefer writing users in [*, &:admin?, *] over users.any?(&:admin?)?

Updated by mame (Yusuke Endoh) 13 days ago Actions #2

  • Status changed from Open to Assigned
  • Assignee set to ktsj (Kazuki Tsujimoto)
Actions

Also available in: PDF Atom