Feature #14763
closedAllow more Enumerable methods to accept method names as symbol arguments
Description
Enumerable has a short hand which accepts method names as symbols.
(5..10).reduce(:+) #=> 45
I'm proposing we allow the same functionality for #any?, #all?, #find, #reject, #select, #one?, #find_index. I'm requesting this because when I did this earlier today and it did not meet my expectations:
[2,4,6].all?(:even?) #=> false
- Any risk of incompatibility?
I don't believe so. As of now, #any? accepts an argument and compares using ===. The following is current behavior:
[Symbol].any?(:even?) #=> false
[:symbol].any?(:even?) #=> false
[].all?(:even?) #=> true
Thanks for consideration of this request. I've used Ruby for 6 years and this is my first feature request. I understand if it is not a high priority or interesting to the maintainers. I am happy to try to add an implementation if it is interesting.
Updated by shevegen (Robert A. Heiler) over 6 years ago
Is there any reason why you did use ":even?" rather
than ":even"?
[2,4,6].all?(:even)
To me symbols with a question mark seem somewhat unusual
or rare.
However had, my question is mostly a small detail, in my
opinion, not the bigger picture. I understand the intent
of the code e. g. query ruby for all even/odd numbers,
through the use of a Symbol.
I am mostly neutral on the proposal itself and slightly in
favour, primarily because I like symbols and I think there
is no problem with it. It should, if accepted, be documented
somewhere though.
My suggestion would be to have matz decide on it soon, simply
so that you can know whether matz is in favour or against it.
I would not worry about the priority - there have been lots of
cases where low priority issues have been added/implemented.
If you would like to, you could add your issue at:
https://bugs.ruby-lang.org/issues/14698
And someone can mention it for discussion at the
developer meeting. That is one of the best ways
to ask matz directly. :)
(I myself won't suggest your issue request because I
think that it is your suggestion, so you should be
in full control over as to whether you want to have
it discussed or not, not me.)
By the way, there have been proposals accepted that
re-use Symbols meaning.
https://github.com/ruby/ruby/blob/trunk/NEWS
See the various Kernel.# methods that use :exception.
I think there is nothing wrong with such special
meanings; just that it should be documented, so
that ruby hackers can know what to use.
Updated by sunnyrjuneja (Sunny Juneja) over 6 years ago
Hey Shevegen,
Thank you for your comments. I really apperciate them! I'll respond inline:
shevegen (Robert A. Heiler) wrote:
Is there any reason why you did use ":even?" rather
than ":even"?[2,4,6].all?(:even)
The only reason I used :even? rather than :even is because Integer has a method called even?. In this case, my example is a shorthand for
[2, 4, 6].all? { |x| x.even? }
I just wanted to show an example with a real method.
I am mostly neutral on the proposal itself and slightly in
favour, primarily because I like symbols and I think there
is no problem with it. It should, if accepted, be documented
somewhere though.
That's a very good point. I forgot to mention it in my original proposal but I wanted to document in the same way inject does. See: http://ruby-doc.org/core-2.5.1/Enumerable.html#method-i-inject
It reads:
inject(sym) -> obj
# Sum some numbers
(5..10).reduce(:+)
If you would like to, you could add your issue at:
Thank you! I had no such idea of such a meeting. I will add it to the issue now.
Updated by sawa (Tsuyoshi Sawada) over 6 years ago
I think reduce
or inject
taking a method name as an argument was convenient in the old days when symbol to proc was not available. Today, this feature of reduce
and inject
does not have the significance it used to have, and should rather be removed because you can just add an ampersand in front of the method name.
Why can't you just add an ampersand in front of the method name? It is not of a big deal.
Updated by sunnyrjuneja (Sunny Juneja) over 6 years ago
sawa (Tsuyoshi Sawada) wrote:
I think
reduce
orinject
taking a method name as an argument was convenient in the old days when symbol to proc was not available. Today, you can just add an ampersand in front of the method name.Why can't you just add an ampersand in front of the method name? It is not of a big deal.
This did not occur to me. Thanks for the reminder. I am happy to remove my request but I am unable to close the issue.
Updated by nobu (Nobuyoshi Nakada) over 6 years ago
I think that this is not acceptable due to the backward compatibility.
[2].all?(:even?)
returns false
now, but it would be true
if this is introduced.
The behavior changes from ===
to send
by the argument class, it doesn't seem very predictable when the argument is not a literal expression.
Updated by ujihisa (Tatsuhiro Ujihisa) about 6 years ago
- Status changed from Open to Closed
closing this on behalf of sunnyrjuneja