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

#add(item) ⇒ Object



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

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

#each(&block) ⇒ Object



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

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