Apply a callback to an array: Difference between revisions

From Rosetta Code
Content added Content deleted
No edit summary
 
No edit summary
Line 1: Line 1:
{{taks}}
{{task}}


Apply a callback function to each element of an Array
Apply a callback function to each element of an Array

Revision as of 20:38, 22 January 2007

Task
Apply a callback to an array
You are encouraged to solve this task according to the task description, using any language you may know.

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