Class: Array

Inherits:
Object
  • Object
show all
Defined in:
lib/array/subindex.rb,
lib/array/subindex/version.rb

Defined Under Namespace

Modules: Subindex

Instance Method Summary collapse

Instance Method Details

#[](index) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/array/subindex.rb', line 4

def [](index)
  if (subindex = index.to_f - index.to_i) > 0
    f = index.to_f.floor
    c = index.to_f.ceil
    f_value = self.fetch(f)
    c_value = self.fetch(c)

    f_value = array_subset(f_value, subindex, :to_end) if
      array_like?(f_value)
    c_value = array_subset(c_value, subindex, :from_beginning) if
      array_like?(c_value)

    if (numeric?(f_value) && numeric?(c_value))
      subindex_as_number(subindex, f_value, c_value)
    else
      subindex_as_string(subindex, f_value, c_value)
    end
  else
    self.fetch(index)
  end
end