Actions
Feature #10829
openAdd to_proc method to the Array class
Status:
Open
Assignee:
-
Target version:
-
Description
In ruby, we've all seen this shortcut:
user.posts.map(&:title)
The expanded version is:
user.posts.map { |post| post.title }
Sometimes, however, that method might take arguments. This feature proposal is to allow the to_proc
shortcut to be able to respond to the Array
class. This would allow developers to be able to use the shortcut to be able to pass in arguments. This can currently be done by reopening the Array
class and supplying it with a to_proc
method:
class Array
def to_proc
proc { |receiver| receiver.send *self }
end
end
This would allow this code to be able to run:
[1, 2, 3, 4, 5].map([:+, 3])
# => [4, 5, 6, 7, 8]
Updated by BenMorganIO (Ben Morgan) over 9 years ago
Update example code, there was a missing &
:
[1, 2, 3, 4, 5].map(&[:+, 3])
# => [4, 5, 6, 7, 8]
Updated by baweaver (Brandon Weaver) over 9 years ago
Have you seen Functors before?
Updated by hsbt (Hiroshi SHIBATA) almost 3 years ago
- Project changed from 14 to Ruby master
Actions
Like0
Like0Like0Like0