Class: Charty::Index

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Includes:
Enumerable
Defined in:
lib/charty/index.rb

Direct Known Subclasses

DaruIndex, PandasIndex, RangeIndex

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(values, name: nil) ⇒ Index

Returns a new instance of Index.


8
9
10
11
# File 'lib/charty/index.rb', line 8

def initialize(values, name: nil)
  @values = values
  @name = name
end

Instance Attribute Details

#nameObject

Returns the value of attribute name.


14
15
16
# File 'lib/charty/index.rb', line 14

def name
  @name
end

#valuesObject (readonly)

Returns the value of attribute values.


13
14
15
# File 'lib/charty/index.rb', line 13

def values
  @values
end

Instance Method Details

#==(other) ⇒ Object


18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/charty/index.rb', line 18

def ==(other)
  case other
  when DaruIndex, PandasIndex
    return false if length != other.length
    to_a == other.to_a
  when Index
    return false if length != other.length
    return true if values == other.values
    to_a == other.to_a
  else
    super
  end
end

#[](i) ⇒ Object


32
33
34
35
36
37
38
39
# File 'lib/charty/index.rb', line 32

def [](i)
  case i
  when 0 ... length
    values[i]
  else
    raise IndexError, "index out of range"
  end
end

#loc(key) ⇒ Object


41
42
43
# File 'lib/charty/index.rb', line 41

def loc(key)
  values.index(key)
end

#union(other) ⇒ Object


45
46
47
48
49
50
51
52
53
# File 'lib/charty/index.rb', line 45

def union(other)
  case other
  when PandasIndex
    index = PandasIndex.try_convert(self)
    return index.union(other) if index
  end

  Index.new(to_a.union(other.to_a), name: name)
end