Issue #8987 has been updated by So Wieso.


Matthew Kerwin wrote:
> On 31 January 2014 15:48, SASADA Koichi <ko1 / atdot.net> wrote:
>  
>  >     p [1, 2, 3, 4].map(&4.method(:+)) #=> [5, 6, 7, 8]
>  >     p [1, 2, 3, 4].map(&{|x| 4+x})  #=> [5, 6, 7, 8]
>  >     p [1, 2, 3, 4].map(&(4, :+))    #=> [5, 6, 7, 8]
>  >     p [1, 2, 3, 4].map(&("4+"))     #=> [5, 6, 7, 8]
>  >     p [1, 2, 3, 4].map(&{4+_})      #=> [5, 6, 7, 8] (doesn't run)
>  >
>  
>  Are any of these actually better than:
>  
>      p [1, 2, 3, 4].map{|x| 4+x }
>  
>  ?
>  
>  
>  -- 
>    Matthew Kerwin
>    http://matthew.kerwin.net.au/

Actually I believe the most readable form would be

`p [1,2,3,4].map{ 4 + _ }  # when there is no ||, set block paramsto _ `

Many blocks in realworld-code are so easy, that it is really a barrier to have to think about a name, and therefore one uses often non-verbose names like you used x. Where is the point in being forced to think of a name, whenyou don't set the name to something meaningful. I guess this was the idea why the to_proc convention was introduced. The problem with it is, that it is really limiting because you cannot use parameters (and the presence of aparameter doesn't necessarily make problems so complex to justify a name).



----------------------------------------
Feature #8987: map/collect extension which handles arguments
https://bugs.ruby-lang.org/issues/8987#change-44856

* Author: So Wieso
* Status: Open
* Priority: Normal
* Assignee: 
* Category: core
* Target version: current: 2.2.0
----------------------------------------
Please consider extending map/collect by allowing additional arguments to be passed to proc, like:
A: [1,2,3,4].map :+, 4
and/or
B: [1,2,3,4].map 4, &:+

=> [5, 6, 7, 8]

Variant A is probably more readable. Variant B is more versatile (allows arbitrary arguments to be passed to block).


---Files--------------------------------
mappi.rb (410 Bytes)


-- 
http://bugs.ruby-lang.org/