Class: Y::Array
Overview
An array can be used to store and retrieve elements.
The array is the replicated counterpart to a Ruby Array. It supports a subset of the Ruby Array operations, like adding, getting and deleting values by position or ranges.
Someone should not instantiate an array directly, but use Doc#get_array instead.
Instance Attribute Summary collapse
-
#document ⇒ Y::Doc
The document this array belongs to.
Instance Method Summary collapse
-
#<<(value, *values) ⇒ void
(also: #push)
Adds an element to the end of the array.
-
#[](index) ⇒ true, ...
Retrieves element at position.
-
#[]=(index, value) ⇒ void
Inserts value at position.
-
#attach(&block) ⇒ Integer
Attach listener to array changes.
-
#concat(*other_arrays) ⇒ void
Adds to array all elements from each Array in
other_arrays
. -
#detach(subscription_id) ⇒ void
Detach listener.
- #each ⇒ void
-
#empty? ⇒ true, false
Check if the array is empty.
-
#first ⇒ true, ...
Returns first element in array if there is at least one.
-
#initialize(doc = nil) ⇒ Array
constructor
Create a new array instance.
-
#last ⇒ true, ...
Returns last element in array if there is at least one element.
-
#pop(n = nil) ⇒ void
Removes last (n) element(s) from array.
-
#shift(n = nil) ⇒ void
Removes first (n) element(s) from array.
-
#size ⇒ Integer
(also: #length)
Size of array.
-
#slice!(*args) ⇒ void
Removes one or more elements from array.
-
#to_a ⇒ Array<true, false, Float, Integer, String, ::Array, Hash>
Convert this array to a Ruby Array.
-
#unshift(value) ⇒ void
(also: #prepend)
Adds an element to the beginning of the array.
Constructor Details
Instance Attribute Details
#document ⇒ Y::Doc
Returns The document this array belongs to.
28 29 30 |
# File 'lib/y/array.rb', line 28 def document @document end |
Instance Method Details
#<<(value, *values) ⇒ void Also known as: push
This method returns an undefined value.
Adds an element to the end of the array
59 60 61 62 63 64 |
# File 'lib/y/array.rb', line 59 def <<(value, *values) document.current_transaction do |tx| yarray_push_back(tx, value) values.each { |v| yarray_push_back(tx, v) } end end |
#[](index) ⇒ true, ...
Retrieves element at position
42 43 44 |
# File 'lib/y/array.rb', line 42 def [](index) document.current_transaction { |tx| yarray_get(tx, index) } end |
#[]=(index, value) ⇒ void
This method returns an undefined value.
Inserts value at position
51 52 53 |
# File 'lib/y/array.rb', line 51 def []=(index, value) document.current_transaction { |tx| yarray_insert(tx, index, value) } end |
#attach(&block) ⇒ Integer
Attach listener to array changes
80 81 82 83 84 |
# File 'lib/y/array.rb', line 80 def attach(&block) raise "provide block" unless block yarray_observe(block.to_proc) end |
#concat(*other_arrays) ⇒ void
This method returns an undefined value.
Adds to array all elements from each Array in other_arrays
.
If one of the arguments isn't an Array, it is silently ignored.
99 100 101 102 103 104 105 106 107 |
# File 'lib/y/array.rb', line 99 def concat(*other_arrays) document.current_transaction do |tx| combined = other_arrays.reduce([]) do |values, arr| values.concat(arr) if arr.is_a?(::Array) end yarray_insert_range(tx, yarray_length(tx), combined) end end |
#detach(subscription_id) ⇒ void
This method returns an undefined value.
Detach listener
113 114 115 |
# File 'lib/y/array.rb', line 113 def detach(subscription_id) yarray_unobserve(subscription_id) end |
#each ⇒ void
This method returns an undefined value.
118 119 120 |
# File 'lib/y/array.rb', line 118 def each(...) document.current_transaction { |tx| yarray_each(tx, ...) } end |
#empty? ⇒ true, false
Check if the array is empty
125 126 127 |
# File 'lib/y/array.rb', line 125 def empty? size.zero? end |
#first ⇒ true, ...
Returns first element in array if there is at least one
132 133 134 |
# File 'lib/y/array.rb', line 132 def first document.current_transaction { |tx| yarray_get(tx, 0) } end |
#last ⇒ true, ...
Returns last element in array if there is at least one element
139 140 141 142 143 144 145 146 |
# File 'lib/y/array.rb', line 139 def last document.current_transaction do |tx| len = yarray_length(tx) return yarray_get(tx, len - 1) if len.positive? nil end end |
#pop(n = nil) ⇒ void
This method returns an undefined value.
Removes last (n) element(s) from array
154 155 156 157 158 159 160 |
# File 'lib/y/array.rb', line 154 def pop(n = nil) document.current_transaction do |tx| len = yarray_length(tx) yarray_remove(tx, len - 1) if n.nil? yarray_remove_range(tx, len - n, n) unless n.nil? end end |
#shift(n = nil) ⇒ void
This method returns an undefined value.
Removes first (n) element(s) from array
172 173 174 175 176 177 178 |
# File 'lib/y/array.rb', line 172 def shift(n = nil) document.current_transaction do |tx| yarray_remove(tx, 0) if n.nil? yarray_remove_range(tx, 0, n) unless nil? end end |
#size ⇒ Integer Also known as: length
Size of array
185 186 187 |
# File 'lib/y/array.rb', line 185 def size document.current_transaction { |tx| yarray_length(tx) } end |
#slice!(n) ⇒ void #slice!(start, length) ⇒ void #slice!(range) ⇒ void
This method returns an undefined value.
Removes one or more elements from array
Attention: In comparison to Array#slice, #slice! will not return the values that got removed. Even this being technically possible, it requires us to read the elements before removing them, which is not desirable in most situations.
222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 |
# File 'lib/y/array.rb', line 222 def slice!(*args) document.current_transaction do |tx| # rubocop:disable Metrics/BlockLength if args.empty? raise ArgumentError, "Provide one of `index`, `range`, `start, length` as arguments" end if args.size == 1 arg = args.first if arg.is_a?(Range) if arg.exclude_end? yarray_remove_range(tx, arg.first, arg.last - arg.first) end unless arg.exclude_end? yarray_remove_range(tx, arg.first, arg.last + 1 - arg.first) end return nil end if arg.is_a?(Numeric) yarray_remove(tx, arg.to_int) return nil end end if args.size == 2 first, second = args if first.is_a?(Numeric) && second.is_a?(Numeric) yarray_remove_range(tx, first, second) return nil end end raise ArgumentError, "Please check your arguments, can't slice." end end |
#to_a ⇒ Array<true, false, Float, Integer, String, ::Array, Hash>
Convert this array to a Ruby Array
268 269 270 |
# File 'lib/y/array.rb', line 268 def to_a document.current_transaction { |tx| yarray_to_a(tx) } end |
#unshift(value) ⇒ void Also known as: prepend
This method returns an undefined value.
Adds an element to the beginning of the array
275 276 277 |
# File 'lib/y/array.rb', line 275 def unshift(value) document.current_transaction { |tx| yarray_push_front(tx, value) } end |