slice_indexes 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.coveralls.yml +1 -0
- data/.gitignore +18 -0
- data/.rspec +2 -0
- data/.travis.yml +9 -0
- data/Gemfile +6 -0
- data/LICENSE.txt +22 -0
- data/README.md +69 -0
- data/Rakefile +1 -0
- data/lib/slice_indexes.rb +2 -0
- data/lib/slice_indexes/extention/array.rb +43 -0
- data/lib/slice_indexes/version.rb +3 -0
- data/slice_indexes.gemspec +25 -0
- data/spec/extention/array_spec.rb +73 -0
- data/spec/spec_helper.rb +6 -0
- metadata +116 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: da432452d133974180c4d8f17d10958d790e3ca0
|
4
|
+
data.tar.gz: 9f8e8731dbe94623158e52395126008a19dc44d2
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 24d8d8d8f85284494b03dbf781b3acea342107e0e79dfd135f90f5583383d4ab04df95e16045a47a9ae56bfc8b93d1b5c68ffadca4e9a89c1f9cf7f20917f8ee
|
7
|
+
data.tar.gz: dea56da814afba04c35c65c5b3c65de3397466cf98a8a914306fa2ff4c5395249b0fba9440ccb4f96c4ffe9b86f2d394eabeb1c2f1cb5af4a71fca2156f19203
|
data/.coveralls.yml
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
service_name: travis-ci
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2014 onigra
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,69 @@
|
|
1
|
+
# Array#slice_indexes
|
2
|
+
|
3
|
+
[![Build Status](https://travis-ci.org/onigra/slice_indexes.svg?branch=master)](https://travis-ci.org/onigra/slice_indexes) [![Coverage Status](https://coveralls.io/repos/onigra/slice_indexes/badge.png)](https://coveralls.io/r/onigra/slice_indexes) [![Code Climate](https://codeclimate.com/github/onigra/slice_indexes.png)](https://codeclimate.com/github/onigra/slice_indexes)
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
```
|
10
|
+
gem 'slice_indexes'
|
11
|
+
```
|
12
|
+
|
13
|
+
And then execute:
|
14
|
+
|
15
|
+
```
|
16
|
+
$ bundle
|
17
|
+
```
|
18
|
+
|
19
|
+
Or install it yourself as:
|
20
|
+
|
21
|
+
```
|
22
|
+
$ gem install to_gal
|
23
|
+
```
|
24
|
+
|
25
|
+
## Usage
|
26
|
+
|
27
|
+
## Array#Indexes
|
28
|
+
|
29
|
+
```rb
|
30
|
+
require 'indexes'
|
31
|
+
|
32
|
+
[1, 2, 3, 1, 2].indexes 1
|
33
|
+
|
34
|
+
#=> [0, 3]
|
35
|
+
|
36
|
+
["foo", "bar", "baz", "foo", "baz", "foo"].indexes "foo"
|
37
|
+
|
38
|
+
#=> [0, 3, 5]
|
39
|
+
|
40
|
+
["foo", 1, 2, "foo", 3, "foo"].indexes { |i| i.class == String }
|
41
|
+
|
42
|
+
#=> [0, 3, 5]
|
43
|
+
```
|
44
|
+
|
45
|
+
## Array#slice_indexes
|
46
|
+
|
47
|
+
```rb
|
48
|
+
require 'indexes'
|
49
|
+
|
50
|
+
[1, 2, 3, 1, 2].slice_indexes 1
|
51
|
+
|
52
|
+
#=> [[1, 2, 3], [1, 2]]
|
53
|
+
|
54
|
+
["foo", "bar", "baz", "foo", "baz", "foo"].slice_indexes "foo"
|
55
|
+
|
56
|
+
#=> [["foo", "bar", "baz"], ["foo", "baz"], ["foo"]]
|
57
|
+
|
58
|
+
["foo", 1, 2, "foo", 3, "foo"].slice_indexes { |i| i.class == String }
|
59
|
+
|
60
|
+
#=> [["foo", 1, 2], ["foo", 3], ["foo"]]
|
61
|
+
```
|
62
|
+
|
63
|
+
## Contributing
|
64
|
+
|
65
|
+
1. Fork it ( http://github.com/onigra/slice_indexes/fork )
|
66
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
67
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
68
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
69
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require "bundler/gem_tasks"
|
@@ -0,0 +1,43 @@
|
|
1
|
+
class Array
|
2
|
+
|
3
|
+
def indexes(item = nil)
|
4
|
+
if block_given?
|
5
|
+
self.each_with_index.map { |obj, index| index if yield(obj) }.reject(&:nil?)
|
6
|
+
else
|
7
|
+
self.each_with_index.map { |obj, index| index if obj == item }.reject(&:nil?)
|
8
|
+
end
|
9
|
+
end
|
10
|
+
|
11
|
+
def slice_indexes(item = nil)
|
12
|
+
result = []
|
13
|
+
tmp = nil
|
14
|
+
|
15
|
+
if block_given?
|
16
|
+
tmp = self.indexes { |i| yield(i) }
|
17
|
+
else
|
18
|
+
tmp = self.indexes item
|
19
|
+
end
|
20
|
+
|
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)]
|
27
|
+
else
|
28
|
+
|
29
|
+
# [1, 1]みたいに取得したい値(item)の値が続いている場合
|
30
|
+
# 取得したindexが連番になっているはず
|
31
|
+
if tmp[index + 1] - obj == 1
|
32
|
+
result << [self[obj]]
|
33
|
+
else
|
34
|
+
|
35
|
+
# objの位置から次のindex - 1までを取得
|
36
|
+
result << self[obj..(tmp[index + 1] - 1)]
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
result
|
42
|
+
end
|
43
|
+
end
|
@@ -0,0 +1,25 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'slice_indexes/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "slice_indexes"
|
8
|
+
spec.version = SliceIndexes::VERSION
|
9
|
+
spec.authors = ["onigra"]
|
10
|
+
spec.email = ["3280467rec@gmail.com"]
|
11
|
+
spec.summary = %q{}
|
12
|
+
spec.description = %q{}
|
13
|
+
spec.homepage = "https://github.com/onigra/slice_indexes"
|
14
|
+
spec.license = "MIT"
|
15
|
+
|
16
|
+
spec.files = `git ls-files -z`.split("\x0")
|
17
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
18
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
19
|
+
spec.require_paths = ["lib"]
|
20
|
+
|
21
|
+
spec.add_development_dependency "bundler", "~> 1.5"
|
22
|
+
spec.add_development_dependency "rake"
|
23
|
+
spec.add_development_dependency "rspec"
|
24
|
+
spec.add_development_dependency "coveralls"
|
25
|
+
end
|
@@ -0,0 +1,73 @@
|
|
1
|
+
require File.expand_path(File.join('../', 'spec_helper'), File.dirname(__FILE__))
|
2
|
+
|
3
|
+
describe Array do
|
4
|
+
describe '#indexes' do
|
5
|
+
context 'case1' do
|
6
|
+
let(:ary) { [1, 2, 3, 1, 2] }
|
7
|
+
subject { ary.indexes 1 }
|
8
|
+
|
9
|
+
it { should eq [0, 3] }
|
10
|
+
end
|
11
|
+
|
12
|
+
context 'case2' do
|
13
|
+
let(:ary) { [1, 2, 3, 1, 2, 1, 1, 3, 2, 1] }
|
14
|
+
subject { ary.indexes 1 }
|
15
|
+
|
16
|
+
it { should eq [0, 3, 5, 6, 9] }
|
17
|
+
end
|
18
|
+
|
19
|
+
context 'case3' do
|
20
|
+
let(:ary) { ["foo", "bar", "baz", "foo", "baz", "foo"] }
|
21
|
+
subject { ary.indexes "foo" }
|
22
|
+
|
23
|
+
it { should eq [0, 3, 5] }
|
24
|
+
end
|
25
|
+
|
26
|
+
context 'case4' do
|
27
|
+
let(:ary) { ["foo", 1, 2, "foo", 3, "foo"] }
|
28
|
+
|
29
|
+
subject do
|
30
|
+
ary.indexes do |i|
|
31
|
+
i.class == String
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
it { should eq [0, 3, 5] }
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
describe '#slice_indexes' do
|
40
|
+
context 'case1' do
|
41
|
+
let(:ary) { [1, 2, 3, 1, 2] }
|
42
|
+
subject { ary.slice_indexes 1 }
|
43
|
+
|
44
|
+
it { should eq [[1, 2, 3], [1, 2]] }
|
45
|
+
end
|
46
|
+
|
47
|
+
context 'case2' do
|
48
|
+
let(:ary) { [1, 2, 3, 1, 2, 1, 1, 3, 2, 1] }
|
49
|
+
subject { ary.slice_indexes 1 }
|
50
|
+
|
51
|
+
it { should eq [[1, 2, 3], [1, 2], [1], [1, 3, 2], [1]] }
|
52
|
+
end
|
53
|
+
|
54
|
+
context 'case3' do
|
55
|
+
let(:ary) { ["foo", "bar", "baz", "foo", "baz", "foo"] }
|
56
|
+
subject { ary.slice_indexes "foo" }
|
57
|
+
|
58
|
+
it { should eq [["foo", "bar", "baz"], ["foo", "baz"], ["foo"]] }
|
59
|
+
end
|
60
|
+
|
61
|
+
context 'case4' do
|
62
|
+
let(:ary) { ["foo", 1, 2, "foo", 3, "foo"] }
|
63
|
+
|
64
|
+
subject do
|
65
|
+
ary.slice_indexes do |i|
|
66
|
+
i.class == String
|
67
|
+
end
|
68
|
+
end
|
69
|
+
|
70
|
+
it { should eq [["foo", 1, 2], ["foo", 3], ["foo"]] }
|
71
|
+
end
|
72
|
+
end
|
73
|
+
end
|
data/spec/spec_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,116 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: slice_indexes
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- onigra
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2014-08-01 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.5'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.5'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ">="
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rspec
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ">="
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ">="
|
53
|
+
- !ruby/object:Gem::Version
|
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
|
+
description: ''
|
70
|
+
email:
|
71
|
+
- 3280467rec@gmail.com
|
72
|
+
executables: []
|
73
|
+
extensions: []
|
74
|
+
extra_rdoc_files: []
|
75
|
+
files:
|
76
|
+
- ".coveralls.yml"
|
77
|
+
- ".gitignore"
|
78
|
+
- ".rspec"
|
79
|
+
- ".travis.yml"
|
80
|
+
- Gemfile
|
81
|
+
- LICENSE.txt
|
82
|
+
- README.md
|
83
|
+
- Rakefile
|
84
|
+
- lib/slice_indexes.rb
|
85
|
+
- lib/slice_indexes/extention/array.rb
|
86
|
+
- lib/slice_indexes/version.rb
|
87
|
+
- slice_indexes.gemspec
|
88
|
+
- spec/extention/array_spec.rb
|
89
|
+
- spec/spec_helper.rb
|
90
|
+
homepage: https://github.com/onigra/slice_indexes
|
91
|
+
licenses:
|
92
|
+
- MIT
|
93
|
+
metadata: {}
|
94
|
+
post_install_message:
|
95
|
+
rdoc_options: []
|
96
|
+
require_paths:
|
97
|
+
- lib
|
98
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
99
|
+
requirements:
|
100
|
+
- - ">="
|
101
|
+
- !ruby/object:Gem::Version
|
102
|
+
version: '0'
|
103
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
104
|
+
requirements:
|
105
|
+
- - ">="
|
106
|
+
- !ruby/object:Gem::Version
|
107
|
+
version: '0'
|
108
|
+
requirements: []
|
109
|
+
rubyforge_project:
|
110
|
+
rubygems_version: 2.2.2
|
111
|
+
signing_key:
|
112
|
+
specification_version: 4
|
113
|
+
summary: ''
|
114
|
+
test_files:
|
115
|
+
- spec/extention/array_spec.rb
|
116
|
+
- spec/spec_helper.rb
|