Class: Range

Inherits:
Object
  • Object
show all
Defined in:
lib/carray/basics.rb

Overview

Reopened to add #to_ca, so a Range can be handed to any CArray entry point that coerces its operand (CArray.cast, wrap_readonly, meshgrid).

Instance Method Summary collapse

Instance Method Details

#to_ca(writable: false) ⇒ CArray

Returns the members of self as a 1-D CA_OBJECT CArray.

Enumeration follows Ruby, so a Float range raises (TypeError, not iterable) and an endless range raises (RangeError); use CArray::DataTypeExtension#linspace or span! for a float axis. A descending integer range is the one departure: it counts down ((3..0).to_ca gives [3, 2, 1, 0], not the empty array (3..0).to_a gives), so that it agrees with the cast form CA_INT32(3..0).

As with Array#to_ca the result is newly built, so writable: true is refused.

Returns:

Raises:

  • (RuntimeError)

    when writable: true is given.



435
436
437
438
439
440
441
442
443
444
445
# File 'lib/carray/basics.rb', line 435

def to_ca(writable: false)
  if writable
    raise "#{self.class}#to_ca builds a new array; " \
          "it can't satisfy `writable: true'"
  end
  first, last = self.begin, self.end
  if first.is_a?(Integer) and last.is_a?(Integer) and first > last
    return CA_OBJECT(self, 1)          # step 1: signed arange
  end
  return CA_OBJECT(self)
end