Class: TypedArray

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of TypedArray.



5
6
7
8
9
10
# File 'lib/scruby/typed_array.rb', line 5

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.



3
4
5
# File 'lib/scruby/typed_array.rb', line 3

def type
  @type
end

Instance Method Details

#+(array) ⇒ Object



13
14
15
# File 'lib/scruby/typed_array.rb', line 13

def +( array )
  TypedArray.new( @type, self.old_plus( array ) )
end

#<<(e) ⇒ Object



23
24
25
26
# File 'lib/scruby/typed_array.rb', line 23

def <<(e)
  check_type_for_obj(e)
  super
end

#[]=(index, e) ⇒ Object



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

def []=(index, e)
  check_type_for_obj(e)
  super
end

#check_array_passed(obj) ⇒ Object

Raises:

  • (TypeError)


52
53
54
# File 'lib/scruby/typed_array.rb', line 52

def check_array_passed(obj)
  raise TypeError.new( "#{obj} is not Array" ) unless obj.instance_of?(Array)
end

#check_type_for_obj(obj) ⇒ Object

:nodoc:

Raises:

  • (TypeError)


60
61
62
# File 'lib/scruby/typed_array.rb', line 60

def check_type_for_obj( obj ) #:nodoc:
  raise TypeError.new("#{obj} is not instance of #{@type}") unless obj.instance_of?(@type)
end

#check_types_for_array(array) ⇒ Object

:nodoc:

Raises:

  • (TypeError)


56
57
58
# File 'lib/scruby/typed_array.rb', line 56

def check_types_for_array( array ) #:nodoc:
  raise TypeError.new("All elements of #{array} should be instance of #{@type}") unless array.reject{ |e| e.instance_of?(@type) }.empty?
end

#compactObject



42
43
44
# File 'lib/scruby/typed_array.rb', line 42

def compact
  self
end

#compact!Object



49
50
# File 'lib/scruby/typed_array.rb', line 49

def compact!
end

#concat(array) ⇒ Object



17
18
19
20
21
# File 'lib/scruby/typed_array.rb', line 17

def concat( array )
  check_array_passed( array )
  check_types_for_array( array )
  super
end

#flattenObject



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

def flatten
  self
end

#flatten!Object



46
47
# File 'lib/scruby/typed_array.rb', line 46

def flatten!
end

#old_plusObject



12
# File 'lib/scruby/typed_array.rb', line 12

alias :old_plus :+

#push(e) ⇒ Object



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

def push(e)
  check_type_for_obj(e)
  super
end