Class: Array
Instance Method Summary collapse
- #convert_boundaries(options = {}) ⇒ Object
-
#delete_rec(obj, &block) ⇒ Object
Deletes items from self and nested arrays that are equal to obj.
-
#flatten_once ⇒ Object
Flattens first level.
- #flatten_with_boundaries(options = {}) ⇒ Object
-
#merge(array, options = {}) ⇒ Object
Merges given array with current one.
- #merge_nested(array) ⇒ Object
-
#sort_by_map(map, attribute = nil) ⇒ Object
Sorts array of values, hashes or objects by given order.
Instance Method Details
#convert_boundaries(options = {}) ⇒ Object
88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 |
# File 'lib/vidibus/core_extensions/array.rb', line 88 def convert_boundaries( = {}) prefix = [:prefix] || "__a" i = 0 boundaries = select {|a| a.match(/#{prefix}\d+/) if a.is_a?(String)} return self unless boundaries.any? array = self.dup converted = [] for boundary in boundaries.uniq converted.concat(array.slice!(0, array.index(boundary))) array.shift converted << array.slice!(0, array.index(boundary)) array.shift end converted.concat(array) end |
#delete_rec(obj, &block) ⇒ Object
Deletes items from self and nested arrays that are equal to obj. If any items are found, returns obj. If the item is not found, returns nil. If the optional code block is given, returns the result of block if the item is not found.
Examples:
list = ["one", "two", ["one"]]
list.delete_rec("one") #=> "one"
list #=> ["two", []]
145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 |
# File 'lib/vidibus/core_extensions/array.rb', line 145 def delete_rec(obj, &block) out = nil _self = self _self.each do |item| _out = nil if item.class == Array _out = item.delete_rec(obj, &block) else _out = _self.delete(obj, &block) end out ||= _out end self.replace(_self) out end |
#flatten_once ⇒ Object
Flattens first level.
Example:
[1, [2, [3]]].flatten_once # => [1, 2, [3]]
Inspired by: snippets.dzone.com/posts/show/302#comment-1417
11 12 13 14 15 16 17 18 19 |
# File 'lib/vidibus/core_extensions/array.rb', line 11 def flatten_once inject([]) do |v,e| if e.is_a?(Array) v.concat(e) else v << e end end end |
#flatten_with_boundaries(options = {}) ⇒ Object
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 |
# File 'lib/vidibus/core_extensions/array.rb', line 72 def flatten_with_boundaries( = {}) prefix = [:prefix] || "__a" i = -1 inject([]) do |v,e| if e.is_a?(Array) i += 1 k = "#{prefix}#{i}" v << k v.concat(e) v << k else v << e end end end |
#merge(array, options = {}) ⇒ Object
Merges given array with current one.
Will perform insertion of new items by three rules:
-
If the item’s predecessor is present, insert item after it.
-
If the item’s follower is present, insert item before it.
-
Insert item at end of list.
Examples:
[].merge([1, 2]) # => [1, 2]
['a'].merge([1, 2]) # => ['a', 1, 2]
[1, 'a'].merge([1, 2]) # => [1, 2, 'a']
[1, 'a'].merge([3, 1, 2]) # => [3, 1, 2, 'a']
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
# File 'lib/vidibus/core_extensions/array.rb', line 35 def merge(array, = {}) strict = [:strict] exclude = [:exclude] || [] list = array.dup for value in list array.delete(value) and next if include?(value) or exclude.include?(value) position = nil if found = merging_predecessor(value, list) position = index(found) + 1 elsif found = merging_follower(value, list) position = index(found) end if position or !strict insert(position || length, value) array.delete(value) end end self end |
#merge_nested(array) ⇒ Object
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 |
# File 'lib/vidibus/core_extensions/array.rb', line 55 def merge_nested(array) combined = self.dup append = [] array.each_with_index do |a,i| source = a.dup combined.each_with_index do |c,j| break if source.empty? existing = combined.flatten_once - c combined[j] = c.merge(source, :strict => true, :exclude => existing) end if source.any? combined[i] ? combined[i].concat(source) : combined.last.concat(source) end end combined end |
#sort_by_map(map, attribute = nil) ⇒ Object
Sorts array of values, hashes or objects by given order. In order to sort hashes or objects, an attribute is required.
Examples:
list = ["two", "one", "three"]
map = ["one", "two", "three"]
list.sort_by_map(map)
# => ["one", "two", "three"]
list = [{:n => "two"}, {:n => "one"}, {:n => "three"}]
map = ["one", "two", "three"]
list.sort_by_map(map, :n)
# => [{:n => "one"}, {:n => "two"}, {:n => "three"}]
119 120 121 122 123 124 125 126 127 128 129 130 131 132 |
# File 'lib/vidibus/core_extensions/array.rb', line 119 def sort_by_map(map, attribute = nil) ordered = [] for a in map ordered << self.detect do |m| if attribute n = m.is_a?(Hash) ? m[attribute] : m.send(attribute) a == n else a == m end end end ordered.compact end |