array-subindex 1.2.0 → 1.3.1

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: c5bd593c29d9bf506cfc8c8c4236c027f643788c
4
- data.tar.gz: 3b500bdf72d283175ad127a0486c4a4d26266913
3
+ metadata.gz: 8f974e1c4e2484751b5cf66c2a52f09d1b03df9c
4
+ data.tar.gz: 2ac3cd24f9f41d389c41b64c9daaed29c913a62c
5
5
  SHA512:
6
- metadata.gz: a082683202f921635b63259829fcdc06bfd791bd36308954d1fb38da092c8abd4ee79382bd05a77650f44879c5f90e129cace7c1e148b95c5c56ab6c37eb6f42
7
- data.tar.gz: cccdcaa249aea013da28564fbccedf398145fce90f735d5cbf2307acc08d4a39c552d5c5892c3aca94f71f3e61fbcbec77f5f5ff531fed0fa510bdadbee7948e
6
+ metadata.gz: 09595abe3cba2f5dbefe2f0e0e167a4dc5e660ee6ff8619edfac2a0f78cc57e9d362b41a8810792ec2cbf1e829c682115b657f092664f030e3490aeb86e08e37
7
+ data.tar.gz: cb9678688923b59857dd31dab7f7beed900b7f11d433df80abab072e9fca0539353f9bf2e10596684a6acbb3f1319ca0eaa902410eceb838fe3e1a27e8163b0d
data/README.md CHANGED
@@ -11,6 +11,8 @@ Now you can, in Ruby at least:
11
11
 
12
12
  %w[ foo bar baz ][1.3] == "arb"
13
13
 
14
+ [8,9][Rational(2,5)] == 8.4
15
+
14
16
  [ [1,2], [3,4] ][0.5] == 2.5
15
17
  ```
16
18
 
@@ -33,6 +35,8 @@ production environment. Clear? Good.
33
35
  require 'array/subindex'
34
36
  ```
35
37
 
38
+ ..and then go pour yourself a stiff drink because, baby, you're going to need it. In fact, you should make it a double just to be safe.
39
+
36
40
  ## Usage
37
41
 
38
42
  This gem overrides the Ruby core Array#[] method. That is insane. If a
@@ -67,6 +71,14 @@ In the case of irrational divisions, the results are rounded:
67
71
  [1,2,3][1.001] == 2.001
68
72
  ```
69
73
 
74
+ You can use other number classes, too:
75
+
76
+ ```ruby
77
+ [8,9][Rational(2,5)] == 8.4
78
+
79
+ [3,4,5][BigDecimal.new("1.5")] == 4.3
80
+ ```
81
+
70
82
  For arrays of strings, a concatination of portions of the values are used:
71
83
 
72
84
  ```ruby
@@ -21,5 +21,5 @@ Gem::Specification.new do |spec|
21
21
  spec.add_development_dependency "bundler", "~> 1.3"
22
22
  spec.add_development_dependency "rake"
23
23
  spec.add_development_dependency "rspec"
24
- # spec.add_development_dependency "debugger"
24
+ spec.add_development_dependency "pry"
25
25
  end
@@ -1,30 +1,55 @@
1
1
  require "array/subindex/version"
2
2
 
3
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
4
+ def [](index, length=nil)
5
+ case index.class.to_s.to_sym
6
+ when :Range
7
+ fetch_range(index)
8
+ when :Float, :Rational
9
+ fetch_subindex(index)
21
10
  else
22
- self.fetch(index)
11
+ fetch_integer_index(index, length)
23
12
  end
24
13
  end
25
14
 
26
15
  private
27
16
 
17
+ def fetch_subindex(index)
18
+ subindex = index.to_f - index.to_i
19
+
20
+ f = index.to_f.floor
21
+ c = index.to_f.ceil
22
+ f_value = self.at(f)
23
+ c_value = self.at(c)
24
+
25
+ f_value = array_subset(f_value, subindex, :to_end) if
26
+ array_like?(f_value)
27
+ c_value = array_subset(c_value, subindex, :from_beginning) if
28
+ array_like?(c_value)
29
+
30
+ if (numeric?(f_value) && numeric?(c_value))
31
+ subindex_as_number(subindex, f_value, c_value)
32
+ else
33
+ subindex_as_string(subindex, f_value, c_value)
34
+ end
35
+ end
36
+
37
+ def fetch_slice(index, length)
38
+ self.slice(index, length.to_i)
39
+ end
40
+
41
+ def fetch_integer_index(index,length)
42
+ if length
43
+ fetch_slice(index, length.to_i)
44
+ else
45
+ self.at(index)
46
+ end
47
+ end
48
+
49
+ def fetch_range(range)
50
+ self.slice(range.first, range.to_a.length)
51
+ end
52
+
28
53
  def subindex_as_number(subindex, f_value, c_value)
29
54
  f_fractional = f_value.to_f * (1.0 - subindex)
30
55
  c_fractional = c_value.to_f * subindex
@@ -51,7 +76,7 @@ private
51
76
  end
52
77
 
53
78
  def array_subset(value, subindex, direction)
54
- value = value.to_a
79
+ value = [*value] # .to_a give deprication warings in 1.8.7
55
80
  subarray = if value.size <= 1
56
81
  value
57
82
  else
@@ -1,5 +1,5 @@
1
1
  class Array
2
2
  module Subindex
3
- VERSION = "1.2.0"
3
+ VERSION = "1.3.1"
4
4
  end
5
5
  end
@@ -5,11 +5,47 @@ describe 'Array::subindex' do
5
5
  describe 'preserve previous functionality' do
6
6
  subject { [1,2,3] }
7
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)
8
+ -1.upto(2) do |i|
9
+ expect( subject[i] ).to eq( subject.fetch(i) )
10
+ expect( subject[i] ).to be_a_kind_of(Fixnum)
11
+ end
12
+ end
13
+
14
+ it "returns normal ranges" do
15
+ expect( subject[0..1] ).to eq( [ 1, 2 ] )
16
+ expect( subject[1..2] ).to eq( [ 2, 3 ] )
17
+ expect( subject[0..2] ).to eq( [ 1, 2, 3 ] )
18
+ end
19
+
20
+ it "returns normal lengths" do
21
+ expect( subject[0,1] ).to eq( [1] )
22
+ expect( subject[0,2] ).to eq( [1,2] )
23
+ expect( subject[1,2] ).to eq( [2,3] )
24
+
25
+ expect( subject[1,0] ).to eq( [] )
26
+ end
27
+ end
11
28
 
12
- expect(subject[-1]).to eq(3)
29
+ context "float with a mantissa of zero" do
30
+
31
+ context "integer values" do
32
+ subject { [ 1, 2 ] }
33
+
34
+ it "returns a single index value as a float" do
35
+ [ 0.0, 1.0].each do |i|
36
+ expect( subject[i] ).to eq( subject.fetch(i) )
37
+ expect( subject[i] ).to be_a_kind_of(Float)
38
+ end
39
+ end
40
+ end
41
+
42
+ context "string values" do
43
+ subject { %w[ foo bar ] }
44
+
45
+ it "returns a single string value" do
46
+ expect( subject[0.0] ).to eq( 'foo' )
47
+ expect( subject[1.0] ).to eq( 'bar' )
48
+ end
13
49
  end
14
50
  end
15
51
 
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: array-subindex
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.2.0
4
+ version: 1.3.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Matthew Nielsen
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2013-12-12 00:00:00.000000000 Z
11
+ date: 2013-12-13 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -52,6 +52,20 @@ dependencies:
52
52
  - - '>='
53
53
  - !ruby/object:Gem::Version
54
54
  version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: pry
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'
55
69
  description: 'Access sub-integer array indexes in Ruby: Array.new([1,2])[0.5] == 1.5'
56
70
  email:
57
71
  - xunker@pyxidis.org