class ELFTools::LazyArray
A helper class for {ELFTools} easy to implement 'lazy loading' objects. Mainly used when loading sections, segments, and symbols.
Public Class Methods
new(size, &block)
click to toggle source
Instantiate a {LazyArray} object. @param [Integer] size
The size of array.
@yieldparam [Integer] i
Needs the +i+-th element.
@yieldreturn [Object]
Value of the +i+-th element.
@example
arr = LazyArray.new(10) { |i| p "calc #{i}"; i * i } p arr[2] # "calc 2" # 4 p arr[3] # "calc 3" # 9 p arr[3] # 9
# File lib/elftools/lazy_array.rb, line 28 def initialize(size, &block) @internal = Array.new(size) @block = block end
Public Instance Methods
[](i)
click to toggle source
To access elements like a normal array.
Elements are lazy loaded at the first time access it. @return [Object]
The element, returned type is the return type of block given in {#initialize}.
# File lib/elftools/lazy_array.rb, line 40 def [](i) # XXX: support negative index? return nil unless i.between?(0, @internal.size - 1) @internal[i] ||= @block.call(i) end