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 = 0) ⇒ 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.
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.
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.
48 49 50 |
# File 'lib/ttfunk/one_based_array.rb', line 48 def each(&block) entries.each(&block) end |
#size ⇒ Integer
Number of elements in this array.
33 34 35 |
# File 'lib/ttfunk/one_based_array.rb', line 33 def size entries.size end |
#to_ary ⇒ Array
Convert to native array.
40 41 42 |
# File 'lib/ttfunk/one_based_array.rb', line 40 def to_ary entries end |