Ruby:Filtering an Array

From Progzoo
Jump to navigation Jump to search

A filter applies a test to each element - it removes any element that fails the test.

Using select

You can use the select (you may also use find_all instead of select) function.

Return only the even numbers in the input list.


[Font] [Default] [Show] [Resize] [History] [Profile]

Manual loop

You can also manually loop through the original list and add desired elements to the resulting list.

Return only the even numbers in the input list.


[Font] [Default] [Show] [Resize] [History] [Profile]