Class: Jsof::WrapArray
- Inherits:
-
Object
- Object
- Jsof::WrapArray
- Includes:
- Enumerable
- Defined in:
- lib/jsof/wrap_array.rb
Overview
Wrapper class for Array.
Instance Method Summary collapse
- #[](index) ⇒ Object
- #[]=(index, val) ⇒ Object
-
#clear_internal ⇒ Object
Clean internal cache.
-
#delete_at(pos) ⇒ Object
Delete element.
- #each ⇒ Object
-
#initialize(obj = [], typeof_element: nil) ⇒ WrapArray
constructor
Create wrapper for Array.
-
#internal_object ⇒ Object
Return reference to wrapped array.
- #size ⇒ Integer
-
#to_a ⇒ Object
Return reference to wrapped array.
-
#to_ary ⇒ Object
Return reference to wrapped array.
- #typeof_element ⇒ Object
Constructor Details
#initialize(obj = [], typeof_element: nil) ⇒ WrapArray
Create wrapper for Array
7 8 9 10 11 |
# File 'lib/jsof/wrap_array.rb', line 7 def initialize(obj = [], typeof_element: nil) @internal_object = obj @wrapped = [] @typeof_element = typeof_element end |
Instance Method Details
#[](index) ⇒ Object
22 23 24 25 26 27 28 29 30 31 |
# File 'lib/jsof/wrap_array.rb', line 22 def [](index) raise ArgumentError unless index.is_a? Integer return @wrapped[index] if @wrapped[index] elem = @internal_object[index] boxed = Jsof::WrapHelper.boxing(elem, typeof_element) @wrapped[index] = boxed if elem != boxed return boxed end |
#[]=(index, val) ⇒ Object
33 34 35 36 37 38 39 40 41 42 43 44 |
# File 'lib/jsof/wrap_array.rb', line 33 def []=(index, val) raise ArgumentError unless index.is_a? Integer if typeof_element raise TypeError unless Jsof::WrapHelper.assignable?(typeof_element, val) end boxed = Jsof::WrapHelper.boxing(val, typeof_element) @wrapped[index] = boxed if Jsof::WrapHelper.wrapped_value?(boxed) @internal_object[index] = Jsof::WrapHelper.unboxing(val) val end |
#clear_internal ⇒ Object
Clean internal cache. Call this after you changed wrapped object directly.
48 49 50 |
# File 'lib/jsof/wrap_array.rb', line 48 def clear_internal @wrapped.clear end |
#delete_at(pos) ⇒ Object
Delete element. (Same as Array#delete_at)
59 60 61 62 |
# File 'lib/jsof/wrap_array.rb', line 59 def delete_at(pos) @internal_object.delete_at(pos) @wrapped.delete_at(pos) end |
#each ⇒ Object
65 66 67 68 69 70 71 72 73 |
# File 'lib/jsof/wrap_array.rb', line 65 def each if block_given? for i in 0...@internal_object.size yield self[i] end else Enumerator.new(self, :each) end end |
#internal_object ⇒ Object
Return reference to wrapped array.
14 15 16 |
# File 'lib/jsof/wrap_array.rb', line 14 def internal_object @internal_object end |
#size ⇒ Integer
53 54 55 |
# File 'lib/jsof/wrap_array.rb', line 53 def size @internal_object.size end |
#to_a ⇒ Object
Return reference to wrapped array.
81 82 83 |
# File 'lib/jsof/wrap_array.rb', line 81 def to_a @internal_object end |
#to_ary ⇒ Object
Return reference to wrapped array.
76 77 78 |
# File 'lib/jsof/wrap_array.rb', line 76 def to_ary @internal_object end |
#typeof_element ⇒ Object
18 19 20 |
# File 'lib/jsof/wrap_array.rb', line 18 def typeof_element @typeof_element end |