Shorter way to pass every element of an array to a function

后端 未结 1 1839
广开言路
广开言路 2021-01-04 04:02

In Ruby, you can apply a map function to every element of an array:

@files.map { |f| f.read) }

For which there is the syntactic sugar:

相关标签:
1条回答
  • 2021-01-04 04:25

    You can do this

    @files.map(&method(:read))
    

    But be aware though about performance.

    0 讨论(0)
提交回复
热议问题