Wednesday, July 1, 2009

Ruby Array

1. Summing elements:
my_array.inject(0){|sum,item| sum + item}

2. Double every element:
my_array.map{|item| item*2 }

3. Find items:
my_array.find_all{|item| item % 3 == 0 }

4a. Sort ASC:
my_array.sort

4b. Sort DESC:
my_array.sort.reverse

No comments: