Class: Array
Overview
Array extension.
Instance Method Summary collapse
-
#merge!(*arrays) ⇒ Array
Merges arrays in place.
-
#remove!(&block) ⇒ Array
Moves selected values outside the array, so returns them.
-
#to_h(mode = nil) ⇒ Hash
New hash.
Instance Method Details
#merge!(*arrays) ⇒ Array
Merges arrays in place. It seems to be rather unuseful, but it’s intended for special cases, for example joining arrays while constructing.
An example (underlying object is extended array):
def initialize(array)
self += array # impossible, will fail
self.merge! array # possible, of sure
end
91 92 93 94 95 |
# File 'lib/hash-utils/array.rb', line 91 def merge!(*arrays) arrays.flatten!(1) arrays.each { |i| self << i } self end |
#remove!(&block) ⇒ Array
Moves selected values outside the array, so returns them.
Works similar to Hash#reject!, but returns removed items instead of remaining items.
21 22 23 24 25 26 27 28 29 30 31 32 33 |
# File 'lib/hash-utils/array.rb', line 21 def remove!(&block) result = [ ] self.reject! do |v| if block.call(v) result << v true else false end end return result end |