[ruby-core:118737] [Ruby master Feature#20657] Allow Enumerable#map(&:method) and #each accept additional parameters for method

Issue #20657 has been reported by ukolovda (Dmitry Ukolov). ---------------------------------------- Feature #20657: Allow Enumerable#map(&:method) and #each accept additional parameters for method https://bugs.ruby-lang.org/issues/20657 * Author: ukolovda (Dmitry Ukolov) * Status: Open ---------------------------------------- I can use `#map` (or `#each`) with short method name, for example: ``` ruby a = ["a1", "b1", "c1"] b = a.map(&:length) # or a.each(&:some_method) ``` But with additional arguments I must use yield: ```ruby c = a.map { |item| item.ljust(10) } # or a.each { |item| item.some_method(arg1, arg2) } ``` Direct sending allow make the code more simple: ```ruby c = a.map(&:ljust, 10) # Now give syntax error, unexpected ',', expecting ')' # or a.each(&:some_method, arg1, arg2) # Now give syntax error, unexpected ',', expecting ')' ``` -- https://bugs.ruby-lang.org/

Issue #20657 has been updated by bkuhlmann (Brooke Kuhlmann). [Method Parameters And Arguments](https://alchemists.io/articles/ruby_method_parameters_and_arguments) are already complex (_powerful_ but complex). Historically, the block parameter has always been at the last position and optional by default. This would introduce new method signature complexity if a block was allowed to appear before positional and/or keyword arguments. What you might want instead, is a refinement to `Symbol#call` as provided by my [Refinements](https://alchemists.io/projects/refinements#_call) gem. Example: ``` ruby using Refinements::Symbol a.map(&:ljust.call(10)) # [ # "a1 ", # "b1 ", # "c1 " # ] ``` This would allow you to achieve what you asking for -- with tad more syntax than what you describe -- without making a drastic change to the language. Anyway, food for thought. ---------------------------------------- Feature #20657: Allow Enumerable#map(&:method) and #each accept additional parameters for method https://bugs.ruby-lang.org/issues/20657#change-109279 * Author: ukolovda (Dmitry Ukolov) * Status: Open ---------------------------------------- I can use `#map` (or `#each`) with short method name, for example: ``` ruby a = ["a1", "b1", "c1"] b = a.map(&:length) # or a.each(&:some_method) ``` But with additional arguments I must use yield: ```ruby c = a.map { |item| item.ljust(10) } # or a.each { |item| item.some_method(arg1, arg2) } ``` Direct sending allow make the code more simple: ```ruby c = a.map(&:ljust, 10) # Now give syntax error, unexpected ',', expecting ')' # or a.each(&:some_method, arg1, arg2) # Now give syntax error, unexpected ',', expecting ')' ``` -- https://bugs.ruby-lang.org/

Issue #20657 has been updated by matz (Yukihiro Matsumoto). As @bkuhlmann mentioned, there's room for the language enhancement here to provide arguments to the method for a block. But not this way. Because the arguments belong to the method, not the method call (each or map in the example). Matz. ---------------------------------------- Feature #20657: Allow Enumerable#map(&:method) and #each accept additional parameters for method https://bugs.ruby-lang.org/issues/20657#change-109281 * Author: ukolovda (Dmitry Ukolov) * Status: Open ---------------------------------------- I can use `#map` (or `#each`) with short method name, for example: ``` ruby a = ["a1", "b1", "c1"] b = a.map(&:length) # or a.each(&:some_method) ``` But with additional arguments I must use yield: ```ruby c = a.map { |item| item.ljust(10) } # or a.each { |item| item.some_method(arg1, arg2) } ``` Direct sending allow make the code more simple: ```ruby c = a.map(&:ljust, 10) # Now give syntax error, unexpected ',', expecting ')' # or a.each(&:some_method, arg1, arg2) # Now give syntax error, unexpected ',', expecting ')' ``` -- https://bugs.ruby-lang.org/

Issue #20657 has been updated by ukolovda (Dmitry Ukolov). Oo, the ampersand is not language syntax, as I think before, but simple operator, converting Symbol to Proc. It is fine trix, but cannot accept additional arguments. I'm disappointed. Thank you for the clarification. ---------------------------------------- Feature #20657: Allow Enumerable#map(&:method) and #each accept additional parameters for method https://bugs.ruby-lang.org/issues/20657#change-109282 * Author: ukolovda (Dmitry Ukolov) * Status: Open ---------------------------------------- I can use `#map` (or `#each`) with short method name, for example: ``` ruby a = ["a1", "b1", "c1"] b = a.map(&:length) # or a.each(&:some_method) ``` But with additional arguments I must use yield: ```ruby c = a.map { |item| item.ljust(10) } # or a.each { |item| item.some_method(arg1, arg2) } ``` Direct sending allow make the code more simple: ```ruby c = a.map(&:ljust, 10) # Now give syntax error, unexpected ',', expecting ')' # or a.each(&:some_method, arg1, arg2) # Now give syntax error, unexpected ',', expecting ')' ``` -- https://bugs.ruby-lang.org/

Issue #20657 has been updated by nobu (Nobuyoshi Nakada). Status changed from Open to Feedback ukolovda (Dmitry Ukolov) wrote in #note-3:
Oo, the ampersand is not language syntax, as I think before, but simple operator, converting Symbol to Proc. It is fine trix, but cannot accept additional arguments.
You mean ampersand+colon(`&:`)? "Additional parameters" feature has been requested many times and in many ways (including the same as your proposal) already, but no acceptable solution. Instead, numbered parameters (`_1`, `_2`, ...) and `it` parameter have been introduced. ---------------------------------------- Feature #20657: Allow Enumerable#map(&:method) and #each accept additional parameters for method https://bugs.ruby-lang.org/issues/20657#change-109295 * Author: ukolovda (Dmitry Ukolov) * Status: Feedback ---------------------------------------- I can use `#map` (or `#each`) with short method name, for example: ``` ruby a = ["a1", "b1", "c1"] b = a.map(&:length) # or a.each(&:some_method) ``` But with additional arguments I must use yield: ```ruby c = a.map { |item| item.ljust(10) } # or a.each { |item| item.some_method(arg1, arg2) } ``` Direct sending allow make the code more simple: ```ruby c = a.map(&:ljust, 10) # Now give syntax error, unexpected ',', expecting ')' # or a.each(&:some_method, arg1, arg2) # Now give syntax error, unexpected ',', expecting ')' ``` -- https://bugs.ruby-lang.org/
participants (4)
-
bkuhlmann (Brooke Kuhlmann)
-
matz (Yukihiro Matsumoto)
-
nobu (Nobuyoshi Nakada)
-
ukolovda (Dmitry Ukolov)