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?
To pass an entire array to a function, only the name of the array is passed as an argument. result = calculateSum(num); However, notice the use of [] in the function definition. This informs the compiler that you are passing a one-dimensional array to the function.
To pass an array to a function, just pass the array as function's parameter (as normal variables), and when we pass an array to a function as an argument, in actual the address of the array in the memory is passed, which is the reference.
The reduce() method executes a user-supplied "reducer" callback function on each element of the array, in order, passing in the return value from the calculation on the preceding element. The final result of running the reducer across all elements of the array is a single value.
You can do this
@files.map(&method(:read))
But be aware though about performance.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With