Class: RestrictedArray
- Inherits:
-
Object
- Object
- RestrictedArray
- Defined in:
- lib/restrictedarray.rb
Instance Attribute Summary collapse
-
#array ⇒ Object
readonly
Returns the value of attribute array.
-
#item_class ⇒ Object
readonly
Returns the value of attribute item_class.
-
#strict ⇒ Object
readonly
Returns the value of attribute strict.
Instance Method Summary collapse
- #add(item) ⇒ Object
- #each(&block) ⇒ Object
-
#initialize(item, strict = true) ⇒ RestrictedArray
constructor
A new instance of RestrictedArray.
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
#array ⇒ Object (readonly)
Returns the value of attribute array.
2 3 4 |
# File 'lib/restrictedarray.rb', line 2 def array @array end |
#item_class ⇒ Object (readonly)
Returns the value of attribute item_class.
2 3 4 |
# File 'lib/restrictedarray.rb', line 2 def item_class @item_class end |
#strict ⇒ Object (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 |