slice_by_indexes 0.0.1 → 0.0.2

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 281aa7ebc42dc1adf2135ab0991818b20db61486
4
- data.tar.gz: 553f23516c7ac00bb6bcfaf4f44e717c976cdfce
3
+ metadata.gz: 066b4e9546990bd02065e7a7db7bc79f88fb8f3f
4
+ data.tar.gz: 44b52c1ce5a1da059b9b77308beed686671ecea5
5
5
  SHA512:
6
- metadata.gz: 4f094c9e0108cf747aad60ea3d9a100e92fa1bdde21fd75db114bfea40acfd207def6f223e91505c62a39c285b0fffb6f5b2b3fed2edb9ee8e41d908e0b2a2ad
7
- data.tar.gz: 68edaad73be9e3dbe6e6a9505b9780973a7d65e770cc055389f5d4cde5dab9c2413c225d1baad26b4163f01c729d087563b149eb450bf43fe71abb95bedd7800
6
+ metadata.gz: 5c87e34b93639a24d63d8168d23106c87b7914019918a3bc125191f8d731639d7b185e2884acbfd94d551d12b06934e9d651cc0e9429e0c3299e148208a3b54c
7
+ data.tar.gz: ac5db6d5f6e56da16b1dcf0109cd13d4b10a0423d45b35e477896faa33ed1bfffcc0ec0ac64924ad83570d3c18322e77a1adf9666af5b1a27635ac20088a40d1
data/.travis.yml CHANGED
@@ -3,6 +3,10 @@ rvm:
3
3
  - 2.0.0
4
4
  - 2.1.1
5
5
  - 2.1.2
6
+ - 2.1.3
7
+ - 2.1.4
8
+ - 2.1.5
9
+ - 2.2.0
6
10
  script: bundle exec rspec
7
11
  notifications:
8
12
  on_success: always
data/Gemfile CHANGED
@@ -1,6 +1,13 @@
1
1
  source 'https://rubygems.org'
2
2
 
3
3
  gemspec
4
+
5
+ group :development do
6
+ gem 'guard'
7
+ gem 'guard-rspec'
8
+ gem 'growl'
9
+ end
10
+
4
11
  group :test do
5
12
  gem 'coveralls', require: false
6
13
  end
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 'indexes'
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 'indexes'
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(item = nil)
3
+ def indexes(match_condition = nil)
4
4
  if block_given?
5
- self.each_with_index.map { |obj, index| index if yield(obj) }.reject(&:nil?)
5
+ self.each_with_index.map { |item, i| i if yield(item) }.reject(&:nil?)
6
6
  else
7
- self.each_with_index.map { |obj, index| index if obj == item }.reject(&:nil?)
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(item = nil)
11
+ def slice_by_indexes(match_condition = nil)
12
12
  result = []
13
- tmp = nil
14
13
 
15
- if block_given?
16
- tmp = self.indexes { |i| yield(i) }
14
+ indexes = if block_given?
15
+ self.indexes { |i| yield(i) }
17
16
  else
18
- tmp = self.indexes item
17
+ self.indexes match_condition
19
18
  end
20
19
 
21
- tmp.each_with_index do |obj, index|
22
- # indexが最後だったら
23
- if index == (tmp.size - 1)
24
-
25
- # objの位置から末尾までを取得
26
- result << self[obj..(self.size - 1)]
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]みたいに取得したい値(item)の値が続いている場合
27
+ #
28
+ # [1, 1]みたいに取得したい値(match_condition)の値が続いている場合
30
29
  # 取得したindexが連番になっているはず
31
- if tmp[index + 1] - obj == 1
32
- result << [self[obj]]
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
@@ -1,3 +1,3 @@
1
1
  module SliceByIndexes
2
- VERSION = "0.0.1"
2
+ VERSION = "0.0.2"
3
3
  end
@@ -21,5 +21,4 @@ Gem::Specification.new do |spec|
21
21
  spec.add_development_dependency "bundler", "~> 1.5"
22
22
  spec.add_development_dependency "rake"
23
23
  spec.add_development_dependency "rspec"
24
- spec.add_development_dependency "coveralls"
25
24
  end
@@ -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.1
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: 2014-08-05 00:00:00.000000000 Z
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.2.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