slice_by_indexes 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 281aa7ebc42dc1adf2135ab0991818b20db61486
4
+ data.tar.gz: 553f23516c7ac00bb6bcfaf4f44e717c976cdfce
5
+ SHA512:
6
+ metadata.gz: 4f094c9e0108cf747aad60ea3d9a100e92fa1bdde21fd75db114bfea40acfd207def6f223e91505c62a39c285b0fffb6f5b2b3fed2edb9ee8e41d908e0b2a2ad
7
+ data.tar.gz: 68edaad73be9e3dbe6e6a9505b9780973a7d65e770cc055389f5d4cde5dab9c2413c225d1baad26b4163f01c729d087563b149eb450bf43fe71abb95bedd7800
data/.coveralls.yml ADDED
@@ -0,0 +1 @@
1
+ service_name: travis-ci
data/.gitignore ADDED
@@ -0,0 +1,18 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
18
+ vendor/bundle
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --color
2
+ --format documentation
data/.travis.yml ADDED
@@ -0,0 +1,9 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.0.0
4
+ - 2.1.1
5
+ - 2.1.2
6
+ script: bundle exec rspec
7
+ notifications:
8
+ on_success: always
9
+ on_failure: always
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gemspec
4
+ group :test do
5
+ gem 'coveralls', require: false
6
+ end
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_by_indexes
2
+
3
+ [![Build Status](https://travis-ci.org/onigra/slice_by_indexes.svg?branch=master)](https://travis-ci.org/onigra/slice_by_indexes) [![Coverage Status](https://coveralls.io/repos/onigra/slice_by_indexes/badge.png)](https://coveralls.io/r/onigra/slice_by_indexes) [![Code Climate](https://codeclimate.com/github/onigra/slice_by_indexes.png)](https://codeclimate.com/github/onigra/slice_by_indexes)
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```
10
+ gem 'slice_by_indexes'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ ```
16
+ $ bundle
17
+ ```
18
+
19
+ Or install it yourself as:
20
+
21
+ ```
22
+ $ gem install slice_by_indexes
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_by_indexes
46
+
47
+ ```rb
48
+ require 'indexes'
49
+
50
+ [1, 2, 3, 1, 2].slice_by_indexes 1
51
+
52
+ #=> [[1, 2, 3], [1, 2]]
53
+
54
+ ["foo", "bar", "baz", "foo", "baz", "foo"].slice_by_indexes "foo"
55
+
56
+ #=> [["foo", "bar", "baz"], ["foo", "baz"], ["foo"]]
57
+
58
+ ["foo", 1, 2, "foo", 3, "foo"].slice_by_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_by_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_by_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,3 @@
1
+ module SliceByIndexes
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,2 @@
1
+ require "slice_by_indexes/version"
2
+ require "slice_by_indexes/extention/array"
@@ -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_by_indexes/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "slice_by_indexes"
8
+ spec.version = SliceByIndexes::VERSION
9
+ spec.authors = ["onigra"]
10
+ spec.email = ["3280467rec@gmail.com"]
11
+ spec.summary = %q{add Array#indexes and Array#slice_by_indexes}
12
+ spec.description = %q{Array#index: returns the index list in array such that the object is == to obj. Array#slice_by_indexes: Array#slice by indexes.}
13
+ spec.homepage = "https://github.com/onigra/slice_by_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_by_indexes' do
40
+ context 'case1' do
41
+ let(:ary) { [1, 2, 3, 1, 2] }
42
+ subject { ary.slice_by_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_by_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_by_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_by_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
@@ -0,0 +1,6 @@
1
+ require 'bundler/setup'
2
+ require 'coveralls'
3
+ Coveralls.wear!
4
+ Bundler.setup
5
+
6
+ require 'slice_by_indexes'
metadata ADDED
@@ -0,0 +1,117 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: slice_by_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-05 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: 'Array#index: returns the index list in array such that the object is
70
+ == to obj. Array#slice_by_indexes: Array#slice by indexes.'
71
+ email:
72
+ - 3280467rec@gmail.com
73
+ executables: []
74
+ extensions: []
75
+ extra_rdoc_files: []
76
+ files:
77
+ - ".coveralls.yml"
78
+ - ".gitignore"
79
+ - ".rspec"
80
+ - ".travis.yml"
81
+ - Gemfile
82
+ - LICENSE.txt
83
+ - README.md
84
+ - Rakefile
85
+ - lib/slice_by_indexes.rb
86
+ - lib/slice_by_indexes/extention/array.rb
87
+ - lib/slice_by_indexes/version.rb
88
+ - slice_by_indexes.gemspec
89
+ - spec/extention/array_spec.rb
90
+ - spec/spec_helper.rb
91
+ homepage: https://github.com/onigra/slice_by_indexes
92
+ licenses:
93
+ - MIT
94
+ metadata: {}
95
+ post_install_message:
96
+ rdoc_options: []
97
+ require_paths:
98
+ - lib
99
+ required_ruby_version: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ">="
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ required_rubygems_version: !ruby/object:Gem::Requirement
105
+ requirements:
106
+ - - ">="
107
+ - !ruby/object:Gem::Version
108
+ version: '0'
109
+ requirements: []
110
+ rubyforge_project:
111
+ rubygems_version: 2.2.2
112
+ signing_key:
113
+ specification_version: 4
114
+ summary: add Array#indexes and Array#slice_by_indexes
115
+ test_files:
116
+ - spec/extention/array_spec.rb
117
+ - spec/spec_helper.rb