Class: TTFunk::OneBasedArray
- Inherits:
-
Object
- Object
- TTFunk::OneBasedArray
- Includes:
- Enumerable
- Defined in:
- lib/ttfunk/one_based_array.rb
Overview
Array with indexing starting at 1.
Instance Method Summary collapse
-
#[](idx) ⇒ any?
Get element by index.
-
#each {|element| ... } ⇒ void
Iterate over elements.
-
#initialize(size) ⇒ OneBasedArray
#initialize(entries) ⇒ OneBasedArray
constructor
A new instance of OneBasedArray.
-
#size ⇒ Integer
Number of elements in this array.
-
#to_ary ⇒ Array
Convert to native array.
Constructor Details
#initialize(size) ⇒ OneBasedArray #initialize(entries) ⇒ OneBasedArray
Returns a new instance of OneBasedArray.
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.
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.
Source Code
lib/ttfunk/one_based_array.rb, line 48
48 | def each(&block) |
49 | entries.each(&block) |
50 | end
|
#size ⇒ Integer
Number of elements in this array.
Source Code
lib/ttfunk/one_based_array.rb, line 33
33 | def size |
34 | entries.size |
35 | end
|
#to_ary ⇒ Array
Convert to native array.
Source Code
lib/ttfunk/one_based_array.rb, line 40
40 | def to_ary |
41 | entries
|
42 | end
|