From: Brandon Weaver Date: 2021-12-02T14:29:21-08:00 Subject: [ruby-core:106422] Re: [Ruby master Feature#18369] users.detect(:name, "Dorian") as shorthand for users.detect { |user| user.name == "Dorian" } --===============1817654664== Content-Type: multipart/alternative; boundary="00000000000017915305d231531e" --00000000000017915305d231531e Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable You know, it would be lovely to have a method like that that takes a pattern matching stanza and produces something which responds to =3D=3D=3D.= That would be a fairly elegant solution to the above question with more flexibility in other parts of applications. On Thu, Dec 2, 2021, 11:41 AM zverok (Victor Shepelev) < noreply@ruby-lang.org> wrote: > Issue #18369 has been updated by zverok (Victor Shepelev). > > > > it would be nice to have syntax that allows to shorten one-line matcher= s > for predicates where they would be commonly used > > TBH, since pattern matching inception I hope for some way of putting > patterns into values=E2=80=94to store them in constants, and, in that cas= e, simple > `grep` will do (if that value would respond to `#=3D=3D=3D` which it shou= ld!): > ```ruby > MY_PATTERN =3D _pm_(first_name: /^J/, age: 18..) > > # ...and then > if value in MY_PATTERN ... > > # ...and, consequently, > people.grep(_pm_(first_name: /^J/, age: 18..)) > ``` > (I am marking the dreamed-of PM constructor as ugly `_pm_` here to > underline it is not a ready proposal, but "something should be here") > > ---------------------------------------- > Feature #18369: users.detect(:name, "Dorian") as shorthand for > users.detect { |user| user.name =3D=3D "Dorian" } > https://bugs.ruby-lang.org/issues/18369#change-95052 > > * Author: dorianmariefr (Dorian Mari=C3=A9) > * Status: Open > * Priority: Normal > ---------------------------------------- > Hi, > > I was thinking I often do things like `collection.detect { |item| > item.attribute =3D=3D value }` and a shorthand like > `collection.detect(:attribute, value)` would be quite useful > > What do you think? > > And I know there is `collection.detect { _1.attribute =3D=3D value }` but= I > try not to use `_1` and this syntax would be shorter and simpler > > Could also apply to other methods like `all?` > (`collection.all?(:attribute, value)`), and basically any Enumerable meth= od > https://rubydoc.info/stdlib/core/Enumerable > > > > -- > https://bugs.ruby-lang.org/ > > Unsubscribe: > > --00000000000017915305d231531e Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable
You know, it would be lovely to have a method like that t= hat takes a pattern matching stanza and produces something which responds t= o =3D=3D=3D. That would be a fairly elegant solution to the above question = with more flexibility in other parts of applications.=C2=A0

On Thu, Dec 2, 2= 021, 11:41 AM zverok (Victor Shepelev) <noreply@ruby-lang.org> wrote:
Issue #18369 has been updated by zverok (Victor Shepelev).


> it would be nice to have syntax that allows to shorten one-line matche= rs for predicates where they would be commonly used

TBH, since pattern matching inception I hope for some way of putting patter= ns into values=E2=80=94to store them in constants, and, in that case, simpl= e `grep` will do (if that value would respond to `#=3D=3D=3D` which it shou= ld!):
```ruby
MY_PATTERN =3D _pm_(first_name: /^J/, age: 18..)

# ...and then
if value in MY_PATTERN ...

# ...and, consequently,
people.grep(_pm_(first_name: /^J/, age: 18..))
```
(I am marking the dreamed-of PM constructor as ugly `_pm_` here to underlin= e it is not a ready proposal, but "something should be here")

----------------------------------------
Feature #18369: users.detect(:name, "Dorian") as shorthand for us= ers.detect { |user| user.name =3D=3D "Dorian" }
https://bugs.ruby-lang.org/issues/183= 69#change-95052

* Author: dorianmariefr (Dorian Mari=C3=A9)
* Status: Open
* Priority: Normal
----------------------------------------
Hi,

I was thinking I often do things like `collection.detect { |item| item.attr= ibute =3D=3D value }` and a shorthand like `collection.detect(:attribute, v= alue)` would be quite useful

What do you think?

And I know there is `collection.detect { _1.attribute =3D=3D value }` but I= try not to use `_1` and this syntax would be shorter and simpler

Could also apply to other methods like `all?` (`collection.all?(:attribute,= value)`), and basically any Enumerable method https://rubydoc.info/stdlib/core/Enumerable



--
https://bugs.ruby-lang.org/

Unsubscribe: <mailto:ruby-core-request@ruby-lang.org?su= bject=3Dunsubscribe>
<http://lists.ruby-lang.or= g/cgi-bin/mailman/options/ruby-core>
--00000000000017915305d231531e-- --===============1817654664== Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: base64 Content-Disposition: inline ClVuc3Vic2NyaWJlOiA8bWFpbHRvOnJ1YnktY29yZS1yZXF1ZXN0QHJ1YnktbGFuZy5vcmc/c3Vi amVjdD11bnN1YnNjcmliZT4KPGh0dHA6Ly9saXN0cy5ydWJ5LWxhbmcub3JnL2NnaS1iaW4vbWFp bG1hbi9vcHRpb25zL3J1YnktY29yZT4K --===============1817654664==--