Apply a callback to an array

From Rosetta Code
Revision as of 20:38, 22 January 2007 by rosettacode>Gfannes
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

Template:Taks

Apply a callback function to each element of an Array

Ruby

 #create the array
 ary = [1,2,3,4,5]
 #create the function (print the square)
 def print_square(i)
   puts i**2
 end
 #ruby
 ary.each do |i|
   print_square(i)
 end
 # prints 1,4,9,16,25