Class: HomogeneousArray

Inherits:
Array
  • Object
show all
Defined in:
lib/symphony/symphony.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Array

#to_homogeneous

Constructor Details

#initialize(of_class, *spec) ⇒ HomogeneousArray

Returns a new instance of HomogeneousArray.



4
5
6
7
8
9
10
11
# File 'lib/symphony/symphony.rb', line 4

def initialize(of_class, *spec)
  of_class = [of_class] if !of_class.is_a?(Array)
  @klass = of_class
  
  spec.flatten.each { |obj| raise_type_error(obj) if !self.acceptable_type?(obj) }
 
  super(*spec)
end

Instance Attribute Details

#klassObject (readonly)

Returns the value of attribute klass.



2
3
4
# File 'lib/symphony/symphony.rb', line 2

def klass
  @klass
end

Instance Method Details

#<<(obj) ⇒ Object



22
23
24
25
# File 'lib/symphony/symphony.rb', line 22

def <<(obj)
  raise_type_error(obj) if !self.acceptable_type?(obj)
  super(obj)
end

#==(ary) ⇒ Object



32
33
34
35
36
37
38
39
40
41
# File 'lib/symphony/symphony.rb', line 32

def ==(ary)
  return false if !ary.respond_to?(:size)
  return false if self.size != ary.size
  
  self.each_with_index do |el, i|
    return false if !(el == ary[i])
  end
  
  return true
end

#[]=(*assign_spec) ⇒ Object



17
18
19
20
# File 'lib/symphony/symphony.rb', line 17

def []=(*assign_spec)
  raise_type_error(assign_spec.last) if !self.acceptable_type?(assign_spec.last)
  super(*assign_spec)
end

#raise_type_error(obj) ⇒ Object

Raises:

  • (TypeError)


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

def raise_type_error(obj)
  raise TypeError, "wrong argument type of #{obj.class} #{obj.inspect} (expected #{self.klass})"
end

#replace(array_of_obj) ⇒ Object



27
28
29
30
# File 'lib/symphony/symphony.rb', line 27

def replace(array_of_obj)
  array_of_obj.each { |obj| raise_type_error(obj) if !self.acceptable_type?(obj) }
  super(array_of_obj)
end