Class: RestrictedArray

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(item, strict = true) ⇒ RestrictedArray

Returns a new instance of RestrictedArray.



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

def initialize(item, strict=true)
  if not strict
    def item_class=(clazz)
      @item_class = clazz
    end
  end
  @array = []
  @array.push(item)
  @item_class = item.class
end

Instance Attribute Details

#arrayObject (readonly)

Returns the value of attribute array.



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

def array
  @array
end

#item_classObject (readonly)

Returns the value of attribute item_class.



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

def item_class
  @item_class
end

#strictObject (readonly)

Returns the value of attribute strict.



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

def strict
  @strict
end

Instance Method Details

#collect(&block) ⇒ Object



26
27
28
# File 'lib/restrictedarray.rb', line 26

def collect(&block)
  @array.collect &block
end

#each(&block) ⇒ Object



20
21
22
# File 'lib/restrictedarray.rb', line 20

def each(&block)
  @array.each &block
end

#map(&block) ⇒ Object



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

def map(&block)
  @array.map &block
end

#push(item) ⇒ Object



13
14
15
16
17
18
19
# File 'lib/restrictedarray.rb', line 13

def push(item)
  if item.is_a? @item_class
    @array.push(item)
  else
    raise "Item is not of type #{@item_class}"
  end
end

#to_aObject



32
33
34
# File 'lib/restrictedarray.rb', line 32

def to_a
  @array
end

#to_sObject



29
30
31
# File 'lib/restrictedarray.rb', line 29

def to_s
  @array
end