Wednesday 5 December 2012

Array#select makes me happy

Array#select makes me happy. Ruby was my first programming language and our marriage is going strong. I could compare #select to methods in other (not as awesome) languages, but I  choose not to. 

This is all I'll say, all you non-Rubyists out there, this is what you are missing: 

array_one = [1, 2, 3, 4, 5]
array_two = array_one.select { |a| a > 2 }

Select iterates through array_one picking the items in the array that return true to the condition passed into #select block.

1.9.3p286 :004 > array_one = [1, 2, 3, 4, 5]
 => [1, 2, 3, 4, 5] 
1.9.3p286 :005 > array_two = array_one.select { |a| a > 2 }
 => [3, 4, 5] 
1.9.3p286 :006 >

No comments:

Post a Comment