Last updated 1 year ago
Was this helpful?
Defining a callback function:
The function will get called for each element:
Same as above, but with the function inlined:
For creating a new array, we can use the Array.map method:
Array.map
func callback(i) { say i**2 }
[1,2,3,4].each(callback)
[1,2,3,4].each{|i| say i**2 }
[1,2,3,4,5].map{|i| i**2 }