Class: Hornetseye::List

Inherits:
Object show all
Defined in:
lib/multiarray/list.rb

Overview

Ruby array supporting array views

See Also:

Instance Method Summary collapse

Constructor Details

#initialize(n, options = {}) ⇒ List

Initialise array

Parameters:

  • n (Integer)

    Number of elements.

  • options (Hash) (defaults to: {})

    a customizable set of options

Options Hash (options):

  • :array (Array) — default: [ nil ] * n

    Existing Ruby array to create an array view.

  • :offset (Integer) — default: 0

    Offset for array view.



31
32
33
34
# File 'lib/multiarray/list.rb', line 31

def initialize( n, options = {} )
  @array = options[ :array ] || [ nil ] * n
  @offset = options[ :offset ] || 0
end

Instance Method Details

#+(offset) ⇒ List

Create array view with specified offset

Parameters:

  • offset (Integer)

    Offset for array view.

Returns:

  • (List)

    The resulting array view.



55
56
57
# File 'lib/multiarray/list.rb', line 55

def +( offset )
  List.new 0, :array => @array, :offset => @offset + offset
end

#inspectString

Display information about this object

Returns:

  • (String)

    String with information about this object (e.g. “List(5)”).



39
40
41
# File 'lib/multiarray/list.rb', line 39

def inspect
  "List(#{@array.size - @offset})"
end

#load(typecode) ⇒ Object

Retrieve value of specified typecode

Parameters:

  • typecode (Class)

    The type of the value.

Returns:

  • (Object)

    The referenced value.



66
67
68
# File 'lib/multiarray/list.rb', line 66

def load( typecode )
  @array[ @offset ]
end

#save(value) ⇒ Object

Store value

Parameters:

  • value (Node)

    Value to store.

Returns:



77
78
79
80
# File 'lib/multiarray/list.rb', line 77

def save( value )
  @array[ @offset ] = value.get
  value
end

#to_sString

Display information about this object

Returns:

  • (String)

    String with information about this object (e.g. “List(5)”).



46
47
48
# File 'lib/multiarray/list.rb', line 46

def to_s
  inspect
end