ruby_array_find_consecutive 0.0.2 → 0.0.3
Sign up to get free protection for your applications and to get access to all the features.
data/README.md
CHANGED
@@ -2,24 +2,36 @@
|
|
2
2
|
|
3
3
|
### Description
|
4
4
|
|
5
|
-
`Array#find_consecutive` is a C extension for Ruby. It returns an array of non-overlapping arrays of consecutive numbers for `self` for a given interval. If no interval argument is provided the default is 1. That's a mouthful, so perhaps an example will help.
|
5
|
+
`Array#find_consecutive` is a C extension for Ruby. It returns an array of non-overlapping arrays of consecutive numbers for `self` for a given interval. If no interval argument is provided the default is 1. You can optionally pass a block that will be yielded each array of consecutive numbers. That's a mouthful, so perhaps an example will help.
|
6
6
|
|
7
7
|
### Usage
|
8
8
|
|
9
9
|
```
|
10
10
|
|
11
11
|
[1, 2, 3, 4, 5].find_consecutive
|
12
|
+
|
12
13
|
# => [[1, 2, 3, 4, 5]]
|
13
14
|
|
14
15
|
[1, 2, 3, 4, 5, 10, 4, 5, 6, 7, 8].find_consecutive
|
16
|
+
|
15
17
|
# => [[1, 2, 3, 4, 5], [4, 5, 6, 7, 8]]
|
16
18
|
|
17
|
-
[1, 2, 3, 4, 6, 8, 10, 12, 14].find_consecutive
|
19
|
+
[1, 2, 3, 4, 6, 8, 10, 12, 14].find_consecutive 2
|
20
|
+
|
18
21
|
# => [[4, 6, 8, 10, 12, 14]]
|
19
22
|
|
20
|
-
[1, 2, 3.5, 5, 6.5, 8, 9, 10, 11.5, 13].find_consecutive
|
23
|
+
[1, 2, 3.5, 5, 6.5, 8, 9, 10, 11.5, 13].find_consecutive 1.5
|
24
|
+
|
21
25
|
# => [[2, 3.5, 5, 6.5, 8], [10, 11.5, 13]]
|
22
26
|
|
27
|
+
[1, 2, 3, 4, 7, 6, 7, 8, 9, 10].find_consecutive do |ary|
|
28
|
+
puts ary.length
|
29
|
+
end
|
30
|
+
|
31
|
+
# => 4
|
32
|
+
# => 5
|
33
|
+
# => [[1, 2, 3, 4], [6, 7, 8, 9, 10]]
|
34
|
+
|
23
35
|
```
|
24
36
|
|
25
37
|
### And...it's fast!
|
@@ -45,7 +45,10 @@ rb_arr_find_consecutive ( int argc, VALUE *argv, VALUE self )
|
|
45
45
|
VALUE c_ary_e = TYPE(ne) == T_FLOAT ? rb_float_new(n) : INT2FIX(n);
|
46
46
|
rb_ary_push(c_ary, c_ary_e);
|
47
47
|
} else {
|
48
|
-
if ( RARRAY_LEN(c_ary) > 1 )
|
48
|
+
if ( RARRAY_LEN(c_ary) > 1 ) {
|
49
|
+
rb_ary_push(r_ary, c_ary);
|
50
|
+
if ( rb_block_given_p() ) rb_yield(c_ary);
|
51
|
+
}
|
49
52
|
c_ary = rb_ary_new();
|
50
53
|
}
|
51
54
|
}
|
@@ -1,6 +1,6 @@
|
|
1
1
|
Gem::Specification.new do |gem|
|
2
2
|
gem.name = 'ruby_array_find_consecutive'
|
3
|
-
gem.version = '0.0.
|
3
|
+
gem.version = '0.0.3'
|
4
4
|
gem.authors = ['Richard Calahan']
|
5
5
|
gem.email = ['richard@calahan.me']
|
6
6
|
gem.description = 'Provides a C level extension to Ruby, Array#find_consecutive'
|
@@ -31,4 +31,14 @@ describe Array do
|
|
31
31
|
it 'finds 0 arrays of consecutive integers incrementing 1' do
|
32
32
|
[0, 1.5, 3, 4.5, 6, 3, 4.5].find_consecutive(1).length.should == 0
|
33
33
|
end
|
34
|
+
|
35
|
+
it 'finds 3 arrays and yields each to a block when given' do
|
36
|
+
a = [1, 2, 3, 5, 6, 7, 8, 7, 9, 10]
|
37
|
+
t_res = a.find_consecutive
|
38
|
+
i = 0
|
39
|
+
a.find_consecutive do |ary|
|
40
|
+
t_res[i].length.should == ary.length
|
41
|
+
i += 1
|
42
|
+
end
|
43
|
+
end
|
34
44
|
end
|