user2398029
user2398029

Reputation: 6937

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

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:

@files.map(&:read)

Is there any equivalent for

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

That is terser, similar to the above?

Upvotes: 21

Views: 1666

Answers (1)

oldergod
oldergod

Reputation: 15010

You can do this

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

But be aware though about performance.

Upvotes: 28

Related Questions