Class: Y::Lib0::TypedArray

Inherits:
Array
  • Object
show all
Defined in:
lib/y/lib0/typed_array.rb

Instance Method Summary collapse

Constructor Details

#initializeTypedArray #initialize(size) ⇒ TypedArray #initialize(typed_array) ⇒ TypedArray #initialize(buffer) ⇒ TypedArray #initialize(buffer, offset) ⇒ TypedArray #initialize(buffer, offset, size) ⇒ TypedArray

Returns a new instance of TypedArray.

Overloads:

  • #initializeTypedArray

    Initialize a TypedArray of size=0

  • #initialize(size) ⇒ TypedArray

    Initialize a TypedArray of given size and initialize with 0's

    Parameters:

  • #initialize(typed_array) ⇒ TypedArray

    Create a new TypedArray from an existing

  • #initialize(buffer) ⇒ TypedArray

    Create a new TypedArray from a buffer. All elements must be valid integers that fit into a single byte (unsigned int). This is not checked at runtime.

  • #initialize(buffer, offset) ⇒ TypedArray

    Create a new TypedArray from a buffer and offset. The projected



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/y/lib0/typed_array.rb', line 20

def initialize(*args) # rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/MethodLength, Metrics/PerceivedComplexity
  if args.empty?
    super()
  elsif args.size == 1 && args.first.is_a?(Numeric)
    super(args.first, 0)
  elsif args.size == 1 && args.first.is_a?(TypedArray)
    super(args.first)
  elsif args.size == 1 && args.first.is_a?(Enumerable)
    super(args.first.to_a)
  elsif args.size == 2 && args.first.is_a?(Enumerable) && args.last.is_a?(Numeric)
    super(args.first.to_a[(args.last)..])
  elsif args.size == 3 && args.first.is_a?(Enumerable) && args[1].is_a?(Numeric) && args.last.is_a?(Numeric)
    super(args.first.to_a[args[1], args.last])
  else
    raise "invalid arguments: [#{args.join(", ")}"
  end
end

Instance Method Details

#replace_with(array, offset = 0) ⇒ Object



38
39
40
41
42
# File 'lib/y/lib0/typed_array.rb', line 38

def replace_with(array, offset = 0)
  array.each_with_index do |element, index|
    self[offset + index] = element
  end
end