[#106341] [Ruby master Bug#18369] users.detect(:name, "Dorian") as shorthand for users.detect { |user| user.name == "Dorian" } — dorianmariefr <noreply@...>
Issue #18369 has been reported by dorianmariefr (Dorian Mari辿).
14 messages
2021/11/30
[#106351] [Ruby master Bug#18371] Release branches (release information in general) — "tenderlovemaking (Aaron Patterson)" <noreply@...>
Issue #18371 has been reported by tenderlovemaking (Aaron Patterson).
7 messages
2021/11/30
[ruby-core:106345] [Ruby master Feature#18369] users.detect(:name, "Dorian") as shorthand for users.detect { |user| user.name == "Dorian" }
From:
"sawa (Tsuyoshi Sawada)" <noreply@...>
Date:
2021-11-30 14:21:54 UTC
List:
ruby-core #106345
Issue #18369 has been updated by sawa (Tsuyoshi Sawada).
I think that is too specific to be a part of Ruby core. I don't think this feature would be accepted.
I think you can define a Proc constructor method for yourself like the following:
```ruby
def attreql k, v
Proc.new{_1.send(k) == v}
end
```
Then, you can do:
```ruby
class A
attr_reader :foo, :bar
def initialize foo, bar
@foo, @bar = foo, bar
end
end
array = [A.new(1, 2), A.new(3, 4), A.new(5, 6)]
array.detect(&attreql(:foo, 3)) # => #<A:0x00007fb751064630 @foo=3, @bar=4>
array.all?(&attreql(:foo, 3)) # => false
```
----------------------------------------
Feature #18369: users.detect(:name, "Dorian") as shorthand for users.detect { |user| user.name == "Dorian" }
https://bugs.ruby-lang.org/issues/18369#change-94982
* Author: dorianmariefr (Dorian Mari辿)
* Status: Open
* Priority: Normal
----------------------------------------
Hi,
I was thinking I often do things like `collection.detect { |item| item.attribute == 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 == 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?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-core>