Project

filter

0.0
No commit activity in last 3 years
No release in over 3 years
== Synopsys <code>Enumerable#filter</code> - extended <code>Enumerable#select</code> == Examples String filter (acts like <code>Enumerable#grep</code>): [1, 2, 3, 'ab'].filter(/a/) # => ['ab'] [1, 2, 3, '3'].filter('3') # => ['3'] You can pass a <code>Proc</code> or <code>Symbol</code>. Methods and blocks are allowed too: [1, 2, 3].filter(&:even?) # => [2] [1, 2, 3].filter(:even?) # => [2] [1, 2, 4].filter { |num| num.even? } # => [2, 4] <code>Enumerable#filter</code> can match against enumerable items attributes. Like this: [1, 2, 3, 4.2].filter :to_i => :even? # => [2, 4] If the block is supplied, each matching element is passed to it, and the block's result is stored in the output array. [1, 2, 4].filter(&:even?) { |n| n + 1 } # => [3, 5] <code>Enumerable#filter</code> also accepts <code>true</code> or <code>false</code> as argument: [0, false, 2, nil].filter(true) # => [0, 2] [0, false, 2, nil].filter(false) # => [false, nil] <code>Enumerable#filter</code> also supports <code>OR</code> operator! Just pass many patterns, they will be joined together with <code>OR</code> operator. [0, 2, 3, 4].filter(:zero?, :odd?) # => [0, 3]
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Development

>= 0
 Project Readme

Synopsys¶ ↑

Enumerable#filter - extended Enumerable#select

Examples¶ ↑

String filter (acts like Enumerable#grep):

[1, 2, 3, 'ab'].filter(/a/)              # => ['ab']
[1, 2, 3, '3'].filter('3')               # => ['3']

You can pass a Proc or Symbol. Methods and blocks are allowed too:

[1, 2, 3].filter(&:even?)                # => [2]
[1, 2, 3].filter(:even?)                 # => [2]
[1, 2, 4].filter { |num| num.even? }     # => [2, 4]

Enumerable#filter can match against enumerable items attributes. Like this:

[1, 2, 3, 4.2].filter :to_i => :even?    # => [2, 4]

If the block is supplied, each matching element is passed to it, and the block’s result is stored in the output array.

[1, 2, 4].filter(&:even?) { |n| n + 1 }  # => [3, 5]

Enumerable#filter also accepts true or false as argument:

[0, false, 2, nil].filter(true)          # => [0, 2]
[0, false, 2, nil].filter(false)         # => [false, nil]

Enumerable#filter also supports OR operator! Just pass many patterns, they will be joined together with OR operator.

[0, 2, 3, 4].filter(:zero?, :odd?)       # => [0, 3]