Class: TTFunk::OneBasedArray

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/ttfunk/one_based_array.rb

Overview

Array with indexing starting at 1.

Instance Method Summary collapse

Constructor Details

#initialize(size) ⇒ OneBasedArray #initialize(entries) ⇒ OneBasedArray

Returns a new instance of OneBasedArray.

Overloads:

  • #initialize(size) ⇒ OneBasedArray

    Parameters:

    • size (Integer)

      number of entries in this array

  • #initialize(entries) ⇒ OneBasedArray

    Parameters:

    • entries (Array)

      an array to take entries from

Source Code
lib/ttfunk/one_based_array.rb, line 12
12
def initialize(size = 0)
13
  @entries = Array.new(size)
14
end

Instance Method Details

#[](idx) ⇒ any?

Get element by index.

Parameters:

  • idx (Integer)

Returns:

  • (any, nil)

Raises:

  • IndexError if index is 0

Source Code
lib/ttfunk/one_based_array.rb, line 21
21
def [](idx)
22
  if idx.zero?
23
    raise IndexError,
24
      "index #{idx} was outside the bounds of the array"
25
  end
26
27
  entries[idx - 1]
28
end

#each {|element| ... } ⇒ void

This method returns an undefined value.

Iterate over elements.

Yield Parameters:

  • element (any)
Source Code
lib/ttfunk/one_based_array.rb, line 48
48
def each(&block)
49
  entries.each(&block)
50
end

#sizeInteger

Number of elements in this array.

Returns:

  • (Integer)
Source Code
lib/ttfunk/one_based_array.rb, line 33
33
def size
34
  entries.size
35
end

#to_aryArray

Convert to native array.

Returns:

  • (Array)
Source Code
lib/ttfunk/one_based_array.rb, line 40
40
def to_ary
41
  entries
42
end