Class: TypedArray

Inherits:
Array show all
Defined in:
lib/scruby/core_ext/typed_array.rb

Overview

Typed array is a kind of Array that only accepts elements of a given Class, it will raise a TypeError if an element of diferent Class is passed to the operation methods or if an Array containing objects of a diferent class is concatenated.

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Array

#collect_with_index, #encode_floats, #peel, #peel!, #to_array, #to_da, #wrap_and_zip, #wrap_to, #wrap_to!

Constructor Details

#initialize(type, elements = []) ⇒ TypedArray

Type a Class or an instance, on the operation methods it will match the argument against the Class or instance’s Class



8
9
10
11
12
13
# File 'lib/scruby/core_ext/typed_array.rb', line 8

def initialize type, elements = []
  @type = type.instance_of?(Class) ? type : type.class
  check_array_passed elements
  check_types_for_array elements
  super elements
end

Instance Attribute Details

#typeObject (readonly)

Returns the value of attribute type.



5
6
7
# File 'lib/scruby/core_ext/typed_array.rb', line 5

def type
  @type
end

Instance Method Details

#<<(e) ⇒ Object



27
28
29
30
# File 'lib/scruby/core_ext/typed_array.rb', line 27

def << e
  check_type_for_obj e
  super
end

#[]=(index, e) ⇒ Object



32
33
34
35
# File 'lib/scruby/core_ext/typed_array.rb', line 32

def []= index, e
  check_type_for_obj e
  super
end

#concat(array) ⇒ Object

alias :old_plus :+ #:nodoc:

def + array

self.class.new @type, self.old_plus( array )

end



21
22
23
24
25
# File 'lib/scruby/core_ext/typed_array.rb', line 21

def concat array
  check_array_passed array
  check_types_for_array array
  super
end

#push(e) ⇒ Object



37
38
39
40
# File 'lib/scruby/core_ext/typed_array.rb', line 37

def push e
  check_type_for_obj e
  super
end