filter 0.0.2 → 0.0.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/README.rdoc +9 -9
- data/filter.gemspec +0 -1
- data/lib/filter.rb +13 -15
- data/lib/filter/version.rb +1 -1
- data/spec/filter_spec.rb +15 -15
- metadata +10 -23
data/README.rdoc
CHANGED
@@ -3,20 +3,20 @@ Enumerable#filter - extended Enumerable#select
|
|
3
3
|
|
4
4
|
== Examples
|
5
5
|
String filter (acts like Enumerable#grep):
|
6
|
-
[1, 2, 3, 'ab'].filter(/a/) # =>
|
7
|
-
[1, 2, 3, '3'].filter('3') # =>
|
6
|
+
[1, 2, 3, 'ab'].filter(/a/) # => ['ab']
|
7
|
+
[1, 2, 3, '3'].filter('3') # => ['3']
|
8
8
|
|
9
9
|
You can pass a <tt>Proc</tt> or <tt>Symbol</tt>. Methods and blocks are allowed too:
|
10
|
-
[1, 2, 3].filter(&:even?) # =>
|
11
|
-
[1, 2, 3].filter(:even?) # =>
|
12
|
-
[1, 2, 4].filter { |num| num.even? } # =>
|
10
|
+
[1, 2, 3].filter(&:even?) # => [2]
|
11
|
+
[1, 2, 3].filter(:even?) # => [2]
|
12
|
+
[1, 2, 4].filter { |num| num.even? } # => [2, 4]
|
13
13
|
|
14
14
|
<tt>Enumerable#filter</tt> can match against enumerable items attributes. Like this:
|
15
|
-
[1, 2, 3, 4.2].filter :to_i => :even? # =>
|
15
|
+
[1, 2, 3, 4.2].filter :to_i => :even? # => [2, 4]
|
16
16
|
|
17
17
|
If the block is supplied, each matching element is passed to it, and the block’s result is stored in the output enumerator.
|
18
|
-
[1, 2, 4].filter(&:even?) { |n| n + 1 } # =>
|
18
|
+
[1, 2, 4].filter(&:even?) { |n| n + 1 } # => [3, 5]
|
19
19
|
|
20
20
|
<tt>Enumerable#filter</tt> also accepts <tt>true</tt> or <tt>false</tt> as argument:
|
21
|
-
[0, false, 2, nil].filter(true) # =>
|
22
|
-
[0, false, 2, nil].filter(false) # =>
|
21
|
+
[0, false, 2, nil].filter(true) # => [0, 2]
|
22
|
+
[0, false, 2, nil].filter(false) # => [false, nil]
|
data/filter.gemspec
CHANGED
data/lib/filter.rb
CHANGED
@@ -1,5 +1,3 @@
|
|
1
|
-
require 'deferred_enum'
|
2
|
-
|
3
1
|
# == Synpsys
|
4
2
|
# Extension for Ruby Enumerable module
|
5
3
|
#
|
@@ -7,12 +5,12 @@ require 'deferred_enum'
|
|
7
5
|
# Extends Enumerable module with +filter+ method - enhanced version of Enumerable#select
|
8
6
|
#
|
9
7
|
# == Examples
|
10
|
-
# [1, 2, 3, 'ab'].filter(/a/) # =>
|
11
|
-
# [1, 2, 3].filter(&:even?) # =>
|
12
|
-
# [1, 2, 3, 4.2].filter :to_i => :even? # =>
|
13
|
-
# [1, 2, 4].filter { |num| num.even? } # =>
|
14
|
-
# [1, 2, 4].filter(:even?) { |n| n + 1 } # =>
|
15
|
-
# [0, false, 2, nil].filter(true) # =>
|
8
|
+
# [1, 2, 3, 'ab'].filter(/a/) # => ['ab']
|
9
|
+
# [1, 2, 3].filter(&:even?) # => [2]
|
10
|
+
# [1, 2, 3, 4.2].filter :to_i => :even? # => [2, 4]
|
11
|
+
# [1, 2, 4].filter { |num| num.even? } # => [2, 4]
|
12
|
+
# [1, 2, 4].filter(:even?) { |n| n + 1 } # => [3, 5]
|
13
|
+
# [0, false, 2, nil].filter(true) # => [0, 2]
|
16
14
|
module Enumerable
|
17
15
|
# Extended Enumerable#select combibed with Enumerable#collect
|
18
16
|
#
|
@@ -25,24 +23,24 @@ module Enumerable
|
|
25
23
|
pattern, block = block, nil if block_given? && pattern.nil?
|
26
24
|
|
27
25
|
filtered = case pattern
|
28
|
-
when NilClass then
|
26
|
+
when NilClass then self
|
29
27
|
|
30
28
|
when Class, Module then
|
31
|
-
|
29
|
+
select do |e|
|
32
30
|
e.is_a?(Class) || e.is_a?(Module) ?
|
33
31
|
e <= pattern :
|
34
32
|
e.is_a?(pattern)
|
35
33
|
end
|
36
34
|
|
37
|
-
when Symbol, Proc, Method then
|
35
|
+
when Symbol, Proc, Method then select(&pattern.to_proc)
|
38
36
|
|
39
37
|
when Array then # enum.filter [:even?, :positive?, :cool?, proc { |n| n < 10 }]
|
40
|
-
pattern.reduce(self
|
38
|
+
pattern.reduce(self) do |chain, local_pattern|
|
41
39
|
chain.select(&local_pattern.to_proc)
|
42
40
|
end
|
43
41
|
|
44
42
|
when Hash then # enum.filter :to_i => :even?, :ceil => :odd?
|
45
|
-
pattern.reduce(self
|
43
|
+
pattern.reduce(self) do |chain, pair|
|
46
44
|
attr, local_pattern = pair.map(&:to_proc)
|
47
45
|
|
48
46
|
chain.select do |element|
|
@@ -50,9 +48,9 @@ module Enumerable
|
|
50
48
|
end
|
51
49
|
end
|
52
50
|
|
53
|
-
when TrueClass, FalseClass then
|
51
|
+
when TrueClass, FalseClass then select { |e| !!e == pattern }
|
54
52
|
|
55
|
-
else
|
53
|
+
else select { |e| pattern === e } # otherwise - String, Regexp, Numeric etc.
|
56
54
|
end
|
57
55
|
|
58
56
|
# also transform elements if block given
|
data/lib/filter/version.rb
CHANGED
data/spec/filter_spec.rb
CHANGED
@@ -1,39 +1,39 @@
|
|
1
1
|
require File.expand_path('../test_helper', __FILE__)
|
2
2
|
|
3
3
|
describe Enumerable do
|
4
|
-
it "should return
|
4
|
+
it "should return Enumerable" do
|
5
5
|
result = [1, 2, 3].filter
|
6
6
|
|
7
|
-
result.should be_a(
|
7
|
+
result.should be_a(Enumerable)
|
8
8
|
end
|
9
9
|
|
10
10
|
it "should return equivalent Enumerator on null input" do
|
11
|
-
[1, 2].filter.
|
11
|
+
[1, 2].filter.should == [1, 2]
|
12
12
|
end
|
13
13
|
|
14
14
|
it "should match regular expressions" do
|
15
|
-
[1, 2, 3, 'ba'].filter(/ba/).
|
15
|
+
[1, 2, 3, 'ba'].filter(/ba/).should == ['ba']
|
16
16
|
end
|
17
17
|
|
18
18
|
it "should match against symbols" do
|
19
|
-
[1, 2, 3].filter(:even?).
|
19
|
+
[1, 2, 3].filter(:even?).should == [2]
|
20
20
|
end
|
21
21
|
|
22
22
|
it "should match against lambdas" do
|
23
|
-
[1, 2, 3].filter(lambda{|n| n.even?}).
|
23
|
+
[1, 2, 3].filter(lambda{|n| n.even?}).should == [2]
|
24
24
|
end
|
25
25
|
|
26
26
|
it "should match against blocks" do
|
27
|
-
[1, 2, 3].filter { |n| n.even? }.
|
27
|
+
[1, 2, 3].filter { |n| n.even? }.should == [2]
|
28
28
|
end
|
29
29
|
|
30
30
|
it "should support #map" do
|
31
|
-
[1, 2, 3].filter(:even?) { |n| n + 1 }.
|
31
|
+
[1, 2, 3].filter(:even?) { |n| n + 1 }.should == [3]
|
32
32
|
end
|
33
33
|
|
34
34
|
it "should match against Hash" do
|
35
|
-
[1, 2, 3, 4.2].filter(:to_i => :even?).
|
36
|
-
[2, 3, 4.2].filter(:to_i => :even?, :ceil => :odd?).
|
35
|
+
[1, 2, 3, 4.2].filter(:to_i => :even?).should == [2, 4.2]
|
36
|
+
[2, 3, 4.2].filter(:to_i => :even?, :ceil => :odd?).should == [4.2]
|
37
37
|
end
|
38
38
|
|
39
39
|
it "should match against Class and Module" do
|
@@ -41,16 +41,16 @@ describe Enumerable do
|
|
41
41
|
m = Module.new {}
|
42
42
|
i = c.new
|
43
43
|
|
44
|
-
[c, i, 1].filter(c).
|
45
|
-
[c, m, i, 1].filter(m).
|
44
|
+
[c, i, 1].filter(c).should == [c, i]
|
45
|
+
[c, m, i, 1].filter(m).should == [m]
|
46
46
|
end
|
47
47
|
|
48
48
|
it "should match against Array" do
|
49
|
-
[0, 1].filter([:even?, :zero?]).
|
49
|
+
[0, 1].filter([:even?, :zero?]).should == [0]
|
50
50
|
end
|
51
51
|
|
52
52
|
it "should match against true or false" do
|
53
|
-
[0, false, 2, nil].filter(true).
|
54
|
-
[0, false, 2, nil].filter(false).
|
53
|
+
[0, false, 2, nil].filter(true).should == [0, 2]
|
54
|
+
[0, false, 2, nil].filter(false).should == [false, nil]
|
55
55
|
end
|
56
56
|
end
|
metadata
CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
|
|
5
5
|
segments:
|
6
6
|
- 0
|
7
7
|
- 0
|
8
|
-
-
|
9
|
-
version: 0.0.
|
8
|
+
- 3
|
9
|
+
version: 0.0.3
|
10
10
|
platform: ruby
|
11
11
|
authors:
|
12
12
|
- Alexey Mikhaylov
|
@@ -14,26 +14,13 @@ autorequire:
|
|
14
14
|
bindir: bin
|
15
15
|
cert_chain: []
|
16
16
|
|
17
|
-
date: 2011-12-
|
17
|
+
date: 2011-12-12 00:00:00 +06:00
|
18
18
|
default_executable:
|
19
19
|
dependencies:
|
20
|
-
- !ruby/object:Gem::Dependency
|
21
|
-
name: deferred_enum
|
22
|
-
prerelease: false
|
23
|
-
requirement: &id001 !ruby/object:Gem::Requirement
|
24
|
-
none: false
|
25
|
-
requirements:
|
26
|
-
- - ">="
|
27
|
-
- !ruby/object:Gem::Version
|
28
|
-
segments:
|
29
|
-
- 0
|
30
|
-
version: "0"
|
31
|
-
type: :runtime
|
32
|
-
version_requirements: *id001
|
33
20
|
- !ruby/object:Gem::Dependency
|
34
21
|
name: rspec
|
35
22
|
prerelease: false
|
36
|
-
requirement: &
|
23
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
37
24
|
none: false
|
38
25
|
requirements:
|
39
26
|
- - ">="
|
@@ -42,15 +29,15 @@ dependencies:
|
|
42
29
|
- 0
|
43
30
|
version: "0"
|
44
31
|
type: :development
|
45
|
-
version_requirements: *
|
32
|
+
version_requirements: *id001
|
46
33
|
description: "== Synopsys\n\
|
47
34
|
Enumerable#filter - extended Enumerable#select\n\n\
|
48
35
|
== Examples\n\
|
49
|
-
String filter (acts like Enumerable#grep):\n [1, 2, 3, 'ab'].filter(/a/) # =>
|
50
|
-
You can pass a <tt>Proc</tt> or <tt>Symbol</tt>. Methods and blocks are allowed too:\n [1, 2, 3].filter(&:even?) # =>
|
51
|
-
<tt>Enumerable#filter</tt> can match against enumerable items attributes. Like this:\n [1, 2, 3, 4.2].filter :to_i => :even? # =>
|
52
|
-
If the block is supplied, each matching element is passed to it, and the block\xE2\x80\x99s result is stored in the output enumerator.\n [1, 2, 4].filter(&:even?) { |n| n + 1 } # =>
|
53
|
-
<tt>Enumerable#filter</tt> also accepts <tt>true</tt> or <tt>false</tt> as argument:\n [0, false, 2, nil].filter(true) # =>
|
36
|
+
String filter (acts like Enumerable#grep):\n [1, 2, 3, 'ab'].filter(/a/) # => ['ab']\n [1, 2, 3, '3'].filter('3') # => ['3']\n\n\
|
37
|
+
You can pass a <tt>Proc</tt> or <tt>Symbol</tt>. Methods and blocks are allowed too:\n [1, 2, 3].filter(&:even?) # => [2]\n [1, 2, 3].filter(:even?) # => [2]\n [1, 2, 4].filter { |num| num.even? } # => [2, 4]\n\n\
|
38
|
+
<tt>Enumerable#filter</tt> can match against enumerable items attributes. Like this:\n [1, 2, 3, 4.2].filter :to_i => :even? # => [2, 4]\n\n\
|
39
|
+
If the block is supplied, each matching element is passed to it, and the block\xE2\x80\x99s result is stored in the output enumerator.\n [1, 2, 4].filter(&:even?) { |n| n + 1 } # => [3, 5]\n\n\
|
40
|
+
<tt>Enumerable#filter</tt> also accepts <tt>true</tt> or <tt>false</tt> as argument:\n [0, false, 2, nil].filter(true) # => [0, 2]\n [0, false, 2, nil].filter(false) # => [false, nil]\n"
|
54
41
|
email:
|
55
42
|
- amikhailov83@gmail.com
|
56
43
|
executables: []
|