From: shelvacu@... Date: 2016-02-29T18:35:22+00:00 Subject: [ruby-core:74059] [Ruby trunk Feature#12115] Add Symbol#call to allow to_proc shorthand with arguments Issue #12115 has been updated by Shel vacu. I agree that there should be some syntax for doing this, but I don't think this is the proper way to do it. Personally, the syntax is confusing to me. I would prefer something like: ~~~ [1,2,16].map(&:to_s(16)) ~~~ This way, the way my brain parses it is that &: is the operator for turning a symbol into a block that calls the method on the argument. However, this would require changes in the parser instead of stdlib. My other concern is that Symbol#call returning a proc feels wrong. It leads to code like this: ~~~ a = :to_s a.call(16).call(15) ~~~ While such code may never be written even if this is implemented, I hope it conveys how odd it feels to have a method named "call" always return a proc which is then meant to be called, instead of calling anything. Would the change from &(:meth_name_as_symbol) to special operator &: followed by method name and optionally arguments (ie. the syntax I used above) break any existing code? ---------------------------------------- Feature #12115: Add Symbol#call to allow to_proc shorthand with arguments https://bugs.ruby-lang.org/issues/12115#change-57211 * Author: Felix B��nemann * Status: Open * Priority: Normal * Assignee: ---------------------------------------- I am a great fan of the `Symbol#to_proc` shorthand when mapping or reducing collections: ```ruby [1,2,16].map(&:to_s) => ["1", "2", "16"] [1,2,16].reduce(&:*) => 32 ``` I often wish it would be possible to pass an argument to the method when doing this, which currently requires a block and is more verbose: ```ruby [1,2,16].map { |n| n.to_s(16) } => ["1", "2", "10"] # active_support example {id: 1, parent_id: nil}.as_json.transform_keys { |k| k.camelize :lower }.to_json => '{"id":1,"parentId":null}' ``` It would be much shorter, if ruby allowed this: ```ruby [1,2,16].map(&:to_s.(16)) => ["1", "2", "10"] # active_support example {id: 1, parent_id: nil}.as_json.transform_keys(&:camelize.(:lower)).to_json => '{"id":1,"parentId":null}' ``` This can be implemented easily, by adding the `Symbol#call` method: ```ruby class Symbol def call(*args, &block) ->(caller, *rest) { caller.send(self, *rest, *args, &block) } end end ``` *Source*: [stackoverflow: Can you supply arguments to the map(&:method) syntax in Ruby?](http://stackoverflow.com/questions/23695653/can-you-supply-arguments-to-the-mapmethod-syntax-in-ruby) **I think this is a rather common use case, so I propose to add `Symbol#call` to the Ruby standard library.** -- https://bugs.ruby-lang.org/ Unsubscribe: