slice_by_indexes 0.0.1 → 0.0.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.travis.yml +4 -0
- data/Gemfile +7 -0
- data/Guardfile +11 -0
- data/README.md +2 -2
- data/lib/slice_by_indexes/extention/array.rb +22 -21
- data/lib/slice_by_indexes/version.rb +1 -1
- data/slice_by_indexes.gemspec +0 -1
- data/spec/extention/array_spec.rb +14 -0
- metadata +4 -17
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 066b4e9546990bd02065e7a7db7bc79f88fb8f3f
|
4
|
+
data.tar.gz: 44b52c1ce5a1da059b9b77308beed686671ecea5
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 5c87e34b93639a24d63d8168d23106c87b7914019918a3bc125191f8d731639d7b185e2884acbfd94d551d12b06934e9d651cc0e9429e0c3299e148208a3b54c
|
7
|
+
data.tar.gz: ac5db6d5f6e56da16b1dcf0109cd13d4b10a0423d45b35e477896faa33ed1bfffcc0ec0ac64924ad83570d3c18322e77a1adf9666af5b1a27635ac20088a40d1
|
data/.travis.yml
CHANGED
data/Gemfile
CHANGED
data/Guardfile
ADDED
@@ -0,0 +1,11 @@
|
|
1
|
+
directories %w(spec lib)
|
2
|
+
notification :growl
|
3
|
+
|
4
|
+
guard :rspec, cmd: "bundle exec rspec" do
|
5
|
+
watch(%r{^spec/.+_spec\.rb$})
|
6
|
+
watch(%r{^spec/support/(.+)\.rb$}) { "spec" }
|
7
|
+
watch('spec/spec_helper.rb') { "spec" }
|
8
|
+
|
9
|
+
watch(%r{^lib/slice_by_indexes/extention/array.rb$}) { |m| "spec/extention/array_spec.rb" }
|
10
|
+
end
|
11
|
+
|
data/README.md
CHANGED
@@ -27,7 +27,7 @@ $ gem install slice_by_indexes
|
|
27
27
|
## Array#indexes
|
28
28
|
|
29
29
|
```rb
|
30
|
-
require '
|
30
|
+
require 'slice_by_indexes'
|
31
31
|
|
32
32
|
[1, 2, 3, 1, 2].indexes 1
|
33
33
|
|
@@ -45,7 +45,7 @@ require 'indexes'
|
|
45
45
|
## Array#slice_by_indexes
|
46
46
|
|
47
47
|
```rb
|
48
|
-
require '
|
48
|
+
require 'slice_by_indexes'
|
49
49
|
|
50
50
|
[1, 2, 3, 1, 2].slice_by_indexes 1
|
51
51
|
|
@@ -1,39 +1,40 @@
|
|
1
1
|
class Array
|
2
2
|
|
3
|
-
def indexes(
|
3
|
+
def indexes(match_condition = nil)
|
4
4
|
if block_given?
|
5
|
-
self.each_with_index.map { |
|
5
|
+
self.each_with_index.map { |item, i| i if yield(item) }.reject(&:nil?)
|
6
6
|
else
|
7
|
-
self.each_with_index.map { |
|
7
|
+
self.each_with_index.map { |item, i| i if item == match_condition }.reject(&:nil?)
|
8
8
|
end
|
9
9
|
end
|
10
10
|
|
11
|
-
def slice_by_indexes(
|
11
|
+
def slice_by_indexes(match_condition = nil)
|
12
12
|
result = []
|
13
|
-
tmp = nil
|
14
13
|
|
15
|
-
if block_given?
|
16
|
-
|
14
|
+
indexes = if block_given?
|
15
|
+
self.indexes { |i| yield(i) }
|
17
16
|
else
|
18
|
-
|
17
|
+
self.indexes match_condition
|
19
18
|
end
|
20
19
|
|
21
|
-
|
22
|
-
#
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
result << self[
|
20
|
+
indexes.each_with_index do |item, i|
|
21
|
+
#
|
22
|
+
# indexが最後だったら、現在の位置から末尾までを取得
|
23
|
+
#
|
24
|
+
if i == (indexes.size - 1)
|
25
|
+
result << self[item..(self.size - 1)]
|
27
26
|
else
|
28
|
-
|
29
|
-
# [1, 1]みたいに取得したい値(
|
27
|
+
#
|
28
|
+
# [1, 1]みたいに取得したい値(match_condition)の値が続いている場合
|
30
29
|
# 取得したindexが連番になっているはず
|
31
|
-
|
32
|
-
|
30
|
+
#
|
31
|
+
# その場合は現在の値を取得
|
32
|
+
# そうでない場合、現在の位置から次の一致条件の値までを取得
|
33
|
+
#
|
34
|
+
if indexes[i + 1] - item == 1
|
35
|
+
result << [self[item]]
|
33
36
|
else
|
34
|
-
|
35
|
-
# objの位置から次のindex - 1までを取得
|
36
|
-
result << self[obj..(tmp[index + 1] - 1)]
|
37
|
+
result << self[item..(indexes[i + 1] - 1)]
|
37
38
|
end
|
38
39
|
end
|
39
40
|
end
|
data/slice_by_indexes.gemspec
CHANGED
@@ -34,6 +34,13 @@ describe Array do
|
|
34
34
|
|
35
35
|
it { should eq [0, 3, 5] }
|
36
36
|
end
|
37
|
+
|
38
|
+
context 'case5' do
|
39
|
+
let(:ary) { ["foo", 1, 2, "foo", 3, "foo"] }
|
40
|
+
subject { ary.indexes }
|
41
|
+
|
42
|
+
it { should eq [] }
|
43
|
+
end
|
37
44
|
end
|
38
45
|
|
39
46
|
describe '#slice_by_indexes' do
|
@@ -69,5 +76,12 @@ describe Array do
|
|
69
76
|
|
70
77
|
it { should eq [["foo", 1, 2], ["foo", 3], ["foo"]] }
|
71
78
|
end
|
79
|
+
|
80
|
+
context 'case5' do
|
81
|
+
let(:ary) { ["foo", 1, 2, "foo", 3, "foo"] }
|
82
|
+
subject { ary.slice_by_indexes }
|
83
|
+
|
84
|
+
it { should eq [] }
|
85
|
+
end
|
72
86
|
end
|
73
87
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: slice_by_indexes
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- onigra
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2015-01-19 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -52,20 +52,6 @@ dependencies:
|
|
52
52
|
- - ">="
|
53
53
|
- !ruby/object:Gem::Version
|
54
54
|
version: '0'
|
55
|
-
- !ruby/object:Gem::Dependency
|
56
|
-
name: coveralls
|
57
|
-
requirement: !ruby/object:Gem::Requirement
|
58
|
-
requirements:
|
59
|
-
- - ">="
|
60
|
-
- !ruby/object:Gem::Version
|
61
|
-
version: '0'
|
62
|
-
type: :development
|
63
|
-
prerelease: false
|
64
|
-
version_requirements: !ruby/object:Gem::Requirement
|
65
|
-
requirements:
|
66
|
-
- - ">="
|
67
|
-
- !ruby/object:Gem::Version
|
68
|
-
version: '0'
|
69
55
|
description: 'Array#index: returns the index list in array such that the object is
|
70
56
|
== to obj. Array#slice_by_indexes: Array#slice by indexes.'
|
71
57
|
email:
|
@@ -79,6 +65,7 @@ files:
|
|
79
65
|
- ".rspec"
|
80
66
|
- ".travis.yml"
|
81
67
|
- Gemfile
|
68
|
+
- Guardfile
|
82
69
|
- LICENSE.txt
|
83
70
|
- README.md
|
84
71
|
- Rakefile
|
@@ -108,7 +95,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
108
95
|
version: '0'
|
109
96
|
requirements: []
|
110
97
|
rubyforge_project:
|
111
|
-
rubygems_version: 2.
|
98
|
+
rubygems_version: 2.4.5
|
112
99
|
signing_key:
|
113
100
|
specification_version: 4
|
114
101
|
summary: add Array#indexes and Array#slice_by_indexes
|