[#73707] [Ruby trunk Misc#12004] Code of Conduct — hanmac@...
Issue #12004 has been updated by Hans Mackowiak.
3 messages
2016/02/05
[#73730] [Ruby trunk Feature#12034] RegExp does not respect file encoding directive — nobu@...
Issue #12034 has been updated by Nobuyoshi Nakada.
3 messages
2016/02/07
[#73746] [Ruby trunk Feature#12034] RegExp does not respect file encoding directive — nobu@...
Issue #12034 has been updated by Nobuyoshi Nakada.
3 messages
2016/02/09
[#73919] [Ruby trunk Feature#11262] Make more objects behave like "Functions" — Ruby-Lang@...
Issue #11262 has been updated by J旦rg W Mittag.
3 messages
2016/02/22
[#74019] [Ruby trunk Bug#12103][Rejected] ruby process hangs while executing regular expression. — duerst@...
Issue #12103 has been updated by Martin D端rst.
3 messages
2016/02/27
[ruby-core:74065] [Ruby trunk Feature#12115] Add Symbol#call to allow to_proc shorthand with arguments
From:
shevegen@...
Date:
2016-02-29 20:45:43 UTC
List:
ruby-core #74065
Issue #12115 has been updated by Robert A. Heiler.
I think there have been many other similar proposals. Nobu linked to other
discussions.
From what I have seen, I think the major problem is coming up with a nice
syntax proposal.
`.map(&:foo)`
is ok because it is short.
Adding implicit arguments to it is harder.
`[1,2,16].map(&:to_s(16))`
Is probably ok. But I am not sure if the parser is happy with it.
`[1,2,16].map(&:to_s.(16))`
Is not good IMHO, the . there is very confusing for me.
There is a slight alternative to .call() which is the []
I like [] a lot, but I think it looks a bit weird too
inside of ().
Perhaps we do not have a syntax that will be better if
we require arguments for &: ?
----------------------------------------
Feature #12115: Add Symbol#call to allow to_proc shorthand with arguments
https://bugs.ruby-lang.org/issues/12115#change-57217
* 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: <mailto:ruby-core-request@ruby-lang.org?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-core>