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



12
13
14
# File 'lib/ttfunk/one_based_array.rb', line 12

def initialize(size = 0)
  @entries = Array.new(size)
end

Instance Method Details

#[](idx) ⇒ any?

Get element by index.

Parameters:

  • idx (Integer)

Returns:

  • (any, nil)

Raises:

  • IndexError if index is 0



21
22
23
24
25
26
27
28
# File 'lib/ttfunk/one_based_array.rb', line 21

def [](idx)
  if idx.zero?
    raise IndexError,
      "index #{idx} was outside the bounds of the array"
  end

  entries[idx - 1]
end

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

This method returns an undefined value.

Iterate over elements.

Yield Parameters:

  • element (any)


48
49
50
# File 'lib/ttfunk/one_based_array.rb', line 48

def each(&block)
  entries.each(&block)
end

#sizeInteger

Number of elements in this array.

Returns:

  • (Integer)


33
34
35
# File 'lib/ttfunk/one_based_array.rb', line 33

def size
  entries.size
end

#to_aryArray

Convert to native array.

Returns:

  • (Array)


40
41
42
# File 'lib/ttfunk/one_based_array.rb', line 40

def to_ary
  entries
end