Apply a callback to an array

Defining a callback function:

func callback(i) { say i**2 }

The function will get called for each element:

[1,2,3,4].each(callback)

Same as above, but with the function inlined:

[1,2,3,4].each{|i| say i**2 }

For creating a new array, we can use the Array.map method:

[1,2,3,4,5].map{|i| i**2 }

Last updated