Class: ELFTools::LazyArray
- Inherits:
-
SimpleDelegator
- Object
- SimpleDelegator
- ELFTools::LazyArray
- Defined in:
- lib/elftools/lazy_array.rb
Overview
Instance Method Summary collapse
-
#[](i) ⇒ Object
To access elements like a normal array.
-
#__getobj__ ⇒ Array
Loads all elements.
-
#initialize(size) {|i| ... } ⇒ LazyArray
constructor
Instantiate a LazyArray object.
-
#size ⇒ Integer
(also: #length)
The size of this array.
Constructor Details
#initialize(size) {|i| ... } ⇒ LazyArray
Instantiate a ELFTools::LazyArray object.
33 34 35 36 37 |
# File 'lib/elftools/lazy_array.rb', line 33 def initialize(size, &block) @array = Array.new(size) super(@array) @block = block end |
Instance Method Details
#[](i) ⇒ Object
To access elements like a normal array.
Elements are lazy loaded at the first time access it.
49 50 51 52 53 |
# File 'lib/elftools/lazy_array.rb', line 49 def [](i) return nil unless i.between?(0, size - 1) @array[i] ||= @block.call(i) end |
#__getobj__ ⇒ Array
75 76 77 78 |
# File 'lib/elftools/lazy_array.rb', line 75 def __getobj__ @array.each_index { |i| self[i] } @array end |
#size ⇒ Integer Also known as: length
The size of this array.
This method never loads any element.
60 61 62 |
# File 'lib/elftools/lazy_array.rb', line 60 def size @array.size end |