array-subindex 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +17 -0
- data/.ruby-gemset +1 -0
- data/.ruby-version +1 -0
- data/.travis.yml +6 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +108 -0
- data/Rakefile +11 -0
- data/array-subindex.gemspec +25 -0
- data/lib/array/subindex.rb +77 -0
- data/lib/array/subindex/version.rb +5 -0
- data/spec/lib/array/subindex_spec.rb +118 -0
- data/spec/spec_helper.rb +3 -0
- metadata +101 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: dce6660ba97882e56b1499cccb5ffaa7b163f56c
|
4
|
+
data.tar.gz: 342d9a596affd1b653231292780a566a4d343cd4
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: b8dfc3d648e07ec48deb68697370b249dca2a8959e6a362bb96f339a05619d9e6b0ca2d68246988bff8c35686b467abadcd5e507387f7eb9ee6f2c1644929b7f
|
7
|
+
data.tar.gz: bd5744c75a8b42d1adf63ba330ea90cb5e0582691f922ac1176b336c33d690299e18aaeae14e49bb7a2317f2ea2ec188ebeed47d9461496b7476d07a4eaeb7bd
|
data/.gitignore
ADDED
data/.ruby-gemset
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
array-subindex
|
data/.ruby-version
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
ruby-2.0.0
|
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2013 Matthew Nielsen
|
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,108 @@
|
|
1
|
+
[![Build Status](https://travis-ci.org/xunker/array-subindex.png?branch=master)](https://travis-ci.org/xunker/array-subindex)
|
2
|
+
# Array::Subindex
|
3
|
+
|
4
|
+
Are you tired of The Man getting you down by only letting you use whole
|
5
|
+
integers as array indexes? Who isn't, dude! Seriously, who is The Man anyway?
|
6
|
+
|
7
|
+
Now you can, in Ruby at least:
|
8
|
+
|
9
|
+
```ruby
|
10
|
+
[1,2,3][0.5] == 1.5
|
11
|
+
|
12
|
+
%w[ foo bar baz ][1.3] == "arb"
|
13
|
+
|
14
|
+
[ [1,2], [3,4] ][0.5] == 2.5
|
15
|
+
```
|
16
|
+
|
17
|
+
The inspiration:
|
18
|
+
|
19
|
+
<blockquote class="twitter-tweet" lang="en"><p>"Should array indices start at 0 or 1? My compromise of 0.5 was rejected without, I thought, proper consideration." — Stan Kelly-Bootle</p>— Usman Masood (@usmanm) <a href="https://twitter.com/usmanm/statuses/410449694603485184">December 10, 2013</a></blockquote><script async src="//platform.twitter.com/widgets.js" charset="utf-8"></script>
|
20
|
+
|
21
|
+
## Installation
|
22
|
+
|
23
|
+
You shouldn't really use this gem. Ever. Really, don't.
|
24
|
+
|
25
|
+
Ok, ok, you can use is as long as you promise to *never* use it in a
|
26
|
+
production environment. Clear? Good.
|
27
|
+
|
28
|
+
$ gem install array-subindex
|
29
|
+
|
30
|
+
..then..
|
31
|
+
|
32
|
+
```ruby
|
33
|
+
require 'array/subindex'
|
34
|
+
```
|
35
|
+
|
36
|
+
## Usage
|
37
|
+
|
38
|
+
This gem overrides the Ruby core Array#[] method. That is insanse. If a
|
39
|
+
"normal" integer is used, the method behaves as normal:
|
40
|
+
|
41
|
+
```ruby
|
42
|
+
[1,2][0] == 1
|
43
|
+
```
|
44
|
+
|
45
|
+
However, if the method detects that a number is passed that is not an integer
|
46
|
+
(but still a valid number class), the method will return an equivilent value
|
47
|
+
from the array.
|
48
|
+
|
49
|
+
In the case of an array of numbers (int, float, real), it gets portions of the
|
50
|
+
adjacent indexes, adds them, and returns them. For example:
|
51
|
+
|
52
|
+
```ruby
|
53
|
+
[1,2,3][1.5] == 2.5
|
54
|
+
# 1/2 of index 2 + 1/2 of index 3
|
55
|
+
```
|
56
|
+
|
57
|
+
It also works for unequal devisions (e.g. indexes other than 0.5):
|
58
|
+
|
59
|
+
```ruby
|
60
|
+
[1,2,3][0.25] == 1.75
|
61
|
+
# 1/4 of index 0 + 3/4 of index 1
|
62
|
+
```
|
63
|
+
|
64
|
+
In the case of irrational divisions, the results are rounded:
|
65
|
+
|
66
|
+
```ruby
|
67
|
+
[1,2,3][1.001] == 2.999
|
68
|
+
but rounded to 2.999
|
69
|
+
```
|
70
|
+
|
71
|
+
For arrays of strings, a concatination of portions of the values are used:
|
72
|
+
|
73
|
+
```ruby
|
74
|
+
[ "this", "is", "a", "test" ][0.5] == "isi"
|
75
|
+
# 1/2 of index 0 ("is") added to half of index 1 ("i")
|
76
|
+
```
|
77
|
+
|
78
|
+
For unevenly dividable strings, the index is rounded down:
|
79
|
+
|
80
|
+
```ruby
|
81
|
+
[ "foo", "bar", "baz" ][0.5] == "oob"
|
82
|
+
```
|
83
|
+
|
84
|
+
_BONUS LEVEL_
|
85
|
+
|
86
|
+
For arrays that contain arrays, hashes, or anything that responds like an
|
87
|
+
array, we will get the appropriate index values in those and then concat
|
88
|
+
or add them as appropriate:
|
89
|
+
|
90
|
+
```ruby
|
91
|
+
[ [ 1, 2 ], [ 3, 4] ][0.5] == 2.5
|
92
|
+
# [2] + [3], then 0.5 of result
|
93
|
+
|
94
|
+
[ [ "foo", "bar" ], [ "baz", "qux" ] ][0.5] == "arb"
|
95
|
+
# 1/2 of "bar" (rounded up to 2/3) + 1/2 of "baz" (rounded down to 1/3)
|
96
|
+
|
97
|
+
# works with hashes, too!
|
98
|
+
[ ['foo'], { bar: 'baz' } ][0.5] == 'oobar'
|
99
|
+
# 1/2 of "foo" round up to 2/3 + hash.to_a -> 1/2 of first value
|
100
|
+
```
|
101
|
+
|
102
|
+
## License
|
103
|
+
|
104
|
+
Distributed under the [WTFPL](https://github.com/rlespinasse/WTFPL) license.
|
105
|
+
|
106
|
+
## Contributing
|
107
|
+
|
108
|
+
1. Don't. Stop encouraging me to commit these kinds of sin.
|
data/Rakefile
ADDED
@@ -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 'array/subindex/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "array-subindex"
|
8
|
+
spec.version = Array::Subindex::VERSION
|
9
|
+
spec.authors = ["Matthew Nielsen"]
|
10
|
+
spec.email = ["xunker@pyxidis.org"]
|
11
|
+
spec.description = %q{Access sub-integer array indexes in Ruby: Array.new([1,2])[0.5] == 1.5}
|
12
|
+
spec.summary = %q{Access sub-integer array indexes in Ruby: Array.new([1,2])[0.5] == 1.5}
|
13
|
+
spec.homepage = "https://github.com/xunker/array-subindex"
|
14
|
+
spec.license = "WTFPL"
|
15
|
+
|
16
|
+
spec.files = `git ls-files`.split($/)
|
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.3"
|
22
|
+
spec.add_development_dependency "rake"
|
23
|
+
spec.add_development_dependency "rspec"
|
24
|
+
# spec.add_development_dependency "debugger"
|
25
|
+
end
|
@@ -0,0 +1,77 @@
|
|
1
|
+
require "array/subindex/version"
|
2
|
+
|
3
|
+
class Array
|
4
|
+
def [](index)
|
5
|
+
if (subindex = index.to_f - index.to_i) > 0
|
6
|
+
f = index.to_f.floor
|
7
|
+
c = index.to_f.ceil
|
8
|
+
f_value = self.fetch(f)
|
9
|
+
c_value = self.fetch(c)
|
10
|
+
|
11
|
+
f_value = array_subset(f_value, subindex, :to_end) if
|
12
|
+
array_like?(f_value)
|
13
|
+
c_value = array_subset(c_value, subindex, :from_beginning) if
|
14
|
+
array_like?(c_value)
|
15
|
+
|
16
|
+
if (numeric?(f_value) && numeric?(c_value))
|
17
|
+
subindex_as_number(subindex, f_value, c_value)
|
18
|
+
else
|
19
|
+
subindex_as_string(subindex, f_value, c_value)
|
20
|
+
end
|
21
|
+
else
|
22
|
+
self.fetch(index)
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
private
|
27
|
+
|
28
|
+
def subindex_as_number(subindex, f_value, c_value)
|
29
|
+
f_fractional = f_value.to_f * subindex
|
30
|
+
c_fractional = c_value.to_f * (1.0 - subindex)
|
31
|
+
f_fractional + c_fractional
|
32
|
+
end
|
33
|
+
|
34
|
+
def subindex_as_string(subindex, f_value, c_value)
|
35
|
+
f_value = f_value.to_s
|
36
|
+
c_value = c_value.to_s
|
37
|
+
f_index = (f_value.length * subindex).to_i
|
38
|
+
c_index = (c_value.length * (1.0 - subindex)).to_i
|
39
|
+
[
|
40
|
+
f_value.slice(f_index, f_value.length-1),
|
41
|
+
c_value.slice(0, c_index)
|
42
|
+
].join
|
43
|
+
end
|
44
|
+
|
45
|
+
def numeric?(test)
|
46
|
+
(test.class != String) && (!!(test.to_s =~ /[\d\.\/]+/))
|
47
|
+
end
|
48
|
+
|
49
|
+
def array_like?(test)
|
50
|
+
test.respond_to?(:to_a)
|
51
|
+
end
|
52
|
+
|
53
|
+
def array_subset(value, subindex, direction)
|
54
|
+
value = value.to_a
|
55
|
+
subarray = if value.size <= 1
|
56
|
+
value
|
57
|
+
else
|
58
|
+
index = (value.length.to_f * subindex).to_i
|
59
|
+
subarray = case direction.to_sym
|
60
|
+
when :to_end
|
61
|
+
value.slice(index, value.size-1)
|
62
|
+
when :from_beginning
|
63
|
+
value.slice(0, index)
|
64
|
+
else
|
65
|
+
raise "unknown direction :#{direction}"
|
66
|
+
end
|
67
|
+
subarray
|
68
|
+
end
|
69
|
+
|
70
|
+
if subarray.all?{ |a| numeric?(a) }
|
71
|
+
subarray.inject(:+)
|
72
|
+
else
|
73
|
+
subarray.join
|
74
|
+
end
|
75
|
+
end
|
76
|
+
|
77
|
+
end
|
@@ -0,0 +1,118 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
require 'array/subindex'
|
4
|
+
describe 'Array::subindex' do
|
5
|
+
describe 'preserve previous functionality' do
|
6
|
+
subject { [1,2,3] }
|
7
|
+
it "returns normal index values with integer indexes" do
|
8
|
+
expect(subject[0]).to eq(1)
|
9
|
+
expect(subject[1]).to eq(2)
|
10
|
+
expect(subject[2]).to eq(3)
|
11
|
+
|
12
|
+
expect(subject[-1]).to eq(3)
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
context "adjacent values are numeric" do
|
17
|
+
subject { [1,2,3] }
|
18
|
+
it "will add parts equally" do
|
19
|
+
# half of index 1 + half of index 2
|
20
|
+
expect(
|
21
|
+
subject[1.5]
|
22
|
+
).to eq( 1.0 + 1.5)
|
23
|
+
end
|
24
|
+
|
25
|
+
it "will add parts unequally" do
|
26
|
+
# 1/4 of index 0 + 3/4 of index 1
|
27
|
+
expect(
|
28
|
+
subject[0.25]
|
29
|
+
).to eq( 0.25 + 1.5)
|
30
|
+
end
|
31
|
+
|
32
|
+
it "will round values" do
|
33
|
+
# expect 0.001 of index 1 and 0.998 index 2, but rounded to 2.999
|
34
|
+
expect(
|
35
|
+
subject[1.001]
|
36
|
+
).to eq( 2.999)
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
context "adjacent values are strings" do
|
41
|
+
subject { %w{ this is a test } }
|
42
|
+
it "concats fractional strings equally" do
|
43
|
+
expect(
|
44
|
+
subject[0.5]
|
45
|
+
).to eq(
|
46
|
+
"isi"
|
47
|
+
)
|
48
|
+
end
|
49
|
+
|
50
|
+
it "concats fractional strings unequally" do
|
51
|
+
expect(
|
52
|
+
subject[0.25]
|
53
|
+
).to eq(
|
54
|
+
"hisi"
|
55
|
+
)
|
56
|
+
end
|
57
|
+
|
58
|
+
it "rounds uneven indexes down" do
|
59
|
+
uneven_length_strings = %w{ foo bar baz }
|
60
|
+
|
61
|
+
expect(
|
62
|
+
uneven_length_strings[0.5]
|
63
|
+
).to eq(
|
64
|
+
"oob"
|
65
|
+
)
|
66
|
+
end
|
67
|
+
end
|
68
|
+
|
69
|
+
context "adjacent values are mix of string and numeric" do
|
70
|
+
subject { ["foo", 100] }
|
71
|
+
|
72
|
+
it "will treat all elements as strings" do
|
73
|
+
expect(
|
74
|
+
subject[0.5]
|
75
|
+
).to eq(
|
76
|
+
"oo1"
|
77
|
+
)
|
78
|
+
end
|
79
|
+
end
|
80
|
+
|
81
|
+
context "array contains elements that are array-like" do
|
82
|
+
context "array-like element is full of numbers" do
|
83
|
+
subject { [ [ 1, 2 ], [ 3, 4] ] }
|
84
|
+
it "returns the array indexes added together" do
|
85
|
+
# [2] + [3], then 0.5 of result
|
86
|
+
expect(
|
87
|
+
subject[0.5]
|
88
|
+
).to eq(
|
89
|
+
2.5
|
90
|
+
)
|
91
|
+
end
|
92
|
+
end
|
93
|
+
|
94
|
+
context "array-like element is not numbers" do
|
95
|
+
subject { [ %w[foo bar], %w[baz qux] ] }
|
96
|
+
it "will return concatenated strings" do
|
97
|
+
expect(
|
98
|
+
subject[0.5]
|
99
|
+
).to eq(
|
100
|
+
"arb"
|
101
|
+
)
|
102
|
+
|
103
|
+
end
|
104
|
+
end
|
105
|
+
end
|
106
|
+
|
107
|
+
context "an adjacent value is numeric/string/array" do
|
108
|
+
subject { [ ['foo'], { :bar => 'baz' } ] }
|
109
|
+
it "will coerce them in to strings" do
|
110
|
+
expect(
|
111
|
+
subject[0.5]
|
112
|
+
).to eq(
|
113
|
+
'oobar'
|
114
|
+
)
|
115
|
+
end
|
116
|
+
end
|
117
|
+
|
118
|
+
end
|
data/spec/spec_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,101 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: array-subindex
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Matthew Nielsen
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2013-12-10 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.3'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ~>
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.3'
|
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
|
+
description: 'Access sub-integer array indexes in Ruby: Array.new([1,2])[0.5] == 1.5'
|
56
|
+
email:
|
57
|
+
- xunker@pyxidis.org
|
58
|
+
executables: []
|
59
|
+
extensions: []
|
60
|
+
extra_rdoc_files: []
|
61
|
+
files:
|
62
|
+
- .gitignore
|
63
|
+
- .ruby-gemset
|
64
|
+
- .ruby-version
|
65
|
+
- .travis.yml
|
66
|
+
- Gemfile
|
67
|
+
- LICENSE.txt
|
68
|
+
- README.md
|
69
|
+
- Rakefile
|
70
|
+
- array-subindex.gemspec
|
71
|
+
- lib/array/subindex.rb
|
72
|
+
- lib/array/subindex/version.rb
|
73
|
+
- spec/lib/array/subindex_spec.rb
|
74
|
+
- spec/spec_helper.rb
|
75
|
+
homepage: https://github.com/xunker/array-subindex
|
76
|
+
licenses:
|
77
|
+
- WTFPL
|
78
|
+
metadata: {}
|
79
|
+
post_install_message:
|
80
|
+
rdoc_options: []
|
81
|
+
require_paths:
|
82
|
+
- lib
|
83
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
84
|
+
requirements:
|
85
|
+
- - '>='
|
86
|
+
- !ruby/object:Gem::Version
|
87
|
+
version: '0'
|
88
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
89
|
+
requirements:
|
90
|
+
- - '>='
|
91
|
+
- !ruby/object:Gem::Version
|
92
|
+
version: '0'
|
93
|
+
requirements: []
|
94
|
+
rubyforge_project:
|
95
|
+
rubygems_version: 2.0.6
|
96
|
+
signing_key:
|
97
|
+
specification_version: 4
|
98
|
+
summary: 'Access sub-integer array indexes in Ruby: Array.new([1,2])[0.5] == 1.5'
|
99
|
+
test_files:
|
100
|
+
- spec/lib/array/subindex_spec.rb
|
101
|
+
- spec/spec_helper.rb
|