Class: Array
- Defined in:
- lib/lite/ruby/array.rb,
lib/lite/ruby/safe/array.rb
Instance Method Summary collapse
- #after(value) ⇒ Object
- #all_after(value) ⇒ Object
- #all_before(value) ⇒ Object
- #assert_all_min_values!(*valid_values) ⇒ Object
- #assert_all_valid_values!(*valid_values) ⇒ Object
- #assert_all_value_presence! ⇒ Object
- #assert_min_values!(*valid_values) ⇒ Object
- #assert_valid_values!(*valid_values) ⇒ Object
- #assert_value_presence! ⇒ Object
- #before(value) ⇒ Object
-
#bury(*args) ⇒ Object
rubocop:disable Metrics/AbcSize, Metrics/BlockNesting, Metrics/MethodLength rubocop:disable Metrics/PerceivedComplexity, Style/GuardClause, Style/IfInsideElse.
- #deep_dup ⇒ Object
-
#delete_first ⇒ Object
rubocop:enable Metrics/PerceivedComplexity, Style/GuardClause, Style/IfInsideElse rubocop:enable Metrics/AbcSize, Metrics/BlockNesting, Metrics/MethodLength.
- #delete_first! ⇒ Object
- #delete_last ⇒ Object
- #delete_last! ⇒ Object
- #delete_values(*args) ⇒ Object
- #demote(value) ⇒ Object
- #demote!(value) ⇒ Object
- #denillify(identity = 0) ⇒ Object
- #denillify!(identity = 0) ⇒ Object
- #divergence(other) ⇒ Object
- #duplicates(minimum = 2) ⇒ Object
- #except(*values) ⇒ Object (also: #reject_values)
- #except!(*values) ⇒ Object (also: #reject_values!)
- #extract! ⇒ Object
- #from(position) ⇒ Object
- #fulfill(value, amount) ⇒ Object
- #groups(number) ⇒ Object
-
#in_groups(number, fill_with = nil, &block) ⇒ Object
rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/MethodLength.
-
#in_groups_of(number, fill_with = nil, &block) ⇒ Object
rubocop:disable Metrics/MethodLength, Style/GuardClause.
- #indexes(value) ⇒ Object (also: #indices)
- #merge(*values) ⇒ Object
- #merge!(*values) ⇒ Object
- #nillify ⇒ Object
- #nillify! ⇒ Object
- #only(*values) ⇒ Object (also: #select_values)
- #only!(*values) ⇒ Object (also: #select_values!)
- #position(value) ⇒ Object
- #positions(value) ⇒ Object
- #probability ⇒ Object
- #promote(value) ⇒ Object
- #promote!(value) ⇒ Object
- #rand_sample(max = nil) ⇒ Object
- #rposition(value) ⇒ Object
- #sample! ⇒ Object
-
#split(value = nil) ⇒ Object
rubocop:disable Metrics/MethodLength, Style/ExplicitBlockArgument.
- #strip ⇒ Object
- #strip! ⇒ Object
- #swap(from, to) ⇒ Object
-
#to(position) ⇒ Object
rubocop:enable Metrics/MethodLength, Style/ExplicitBlockArgument.
- #to_sentence(options = {}) ⇒ Object
Instance Method Details
#after(value) ⇒ Object
58 59 60 61 62 |
# File 'lib/lite/ruby/array.rb', line 58 def after(value) return unless include?(value) self[(index(value) + 1) % size] end |
#all_after(value) ⇒ Object
64 65 66 67 68 69 70 71 |
# File 'lib/lite/ruby/array.rb', line 64 def all_after(value) return unless include?(value) i = index(value) return if i == (size - 1) self[(i + 1)..-1] end |
#all_before(value) ⇒ Object
73 74 75 76 77 78 79 80 |
# File 'lib/lite/ruby/array.rb', line 73 def all_before(value) return unless include?(value) i = index(value) return if i.zero? self[0..(i - 1)] end |
#assert_all_min_values!(*valid_values) ⇒ Object
22 23 24 25 26 |
# File 'lib/lite/ruby/array.rb', line 22 def assert_all_min_values!(*valid_values) return assert_min_values!(*valid_values) unless empty? raise ArgumentError, 'An empty array is not allowed' end |
#assert_all_valid_values!(*valid_values) ⇒ Object
38 39 40 41 42 |
# File 'lib/lite/ruby/array.rb', line 38 def assert_all_valid_values!(*valid_values) return assert_valid_values!(*valid_values) unless empty? raise ArgumentError, 'An empty array is not allowed' end |
#assert_all_value_presence! ⇒ Object
52 53 54 55 56 |
# File 'lib/lite/ruby/array.rb', line 52 def assert_all_value_presence! return assert_value_presence! unless empty? raise ArgumentError, 'An empty array is not allowed' end |
#assert_min_values!(*valid_values) ⇒ Object
8 9 10 11 12 13 14 15 16 17 18 19 20 |
# File 'lib/lite/ruby/array.rb', line 8 def assert_min_values!(*valid_values) return self if empty? valid_values.each do |value| next if include?(value) raise ArgumentError, "Missing value: #{value.inspect}. " \ "Minimum values are: #{valid_values.map(&:inspect).join(', ')}" end self end |
#assert_valid_values!(*valid_values) ⇒ Object
28 29 30 31 32 33 34 35 36 |
# File 'lib/lite/ruby/array.rb', line 28 def assert_valid_values!(*valid_values) each do |value| next if valid_values.include?(value) raise ArgumentError, "Invalid value: #{value.inspect}. " \ "Allowed values are: #{valid_values.map(&:inspect).join(', ')}" end end |
#assert_value_presence! ⇒ Object
44 45 46 47 48 49 50 |
# File 'lib/lite/ruby/array.rb', line 44 def assert_value_presence! each do |value| next if value.respond_to?(:present?) ? value.present? : value raise ArgumentError, "A #{value.inspect} value is not allowed" end end |
#before(value) ⇒ Object
82 83 84 85 86 |
# File 'lib/lite/ruby/array.rb', line 82 def before(value) return unless include?(value) self[(index(value) - 1) % size] end |
#bury(*args) ⇒ Object
rubocop:disable Metrics/AbcSize, Metrics/BlockNesting, Metrics/MethodLength rubocop:disable Metrics/PerceivedComplexity, Style/GuardClause, Style/IfInsideElse
90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 |
# File 'lib/lite/ruby/array.rb', line 90 def bury(*args) if args.count < 2 raise ArgumentError, '2 or more arguments required' elsif args.count == 2 if args[0].is_a?(Integer) self[args[0]] = args[1] else self << { args[0] => args[1] } end else if args[0].is_a?(Integer) arg = args.shift self[arg] = [] unless self[arg] self[arg].bury(*args) else self << {}.bury(*args) end end self end |
#deep_dup ⇒ Object
5 6 7 |
# File 'lib/lite/ruby/safe/array.rb', line 5 def deep_dup map(&:deep_dup) end |
#delete_first ⇒ Object
rubocop:enable Metrics/PerceivedComplexity, Style/GuardClause, Style/IfInsideElse rubocop:enable Metrics/AbcSize, Metrics/BlockNesting, Metrics/MethodLength
114 115 116 |
# File 'lib/lite/ruby/array.rb', line 114 def delete_first self[1..-1] end |
#delete_first! ⇒ Object
118 119 120 |
# File 'lib/lite/ruby/array.rb', line 118 def delete_first! replace(delete_first) end |
#delete_last ⇒ Object
122 123 124 |
# File 'lib/lite/ruby/array.rb', line 122 def delete_last self[0...-1] end |
#delete_last! ⇒ Object
126 127 128 |
# File 'lib/lite/ruby/array.rb', line 126 def delete_last! replace(delete_last) end |
#delete_values(*args) ⇒ Object
130 131 132 |
# File 'lib/lite/ruby/array.rb', line 130 def delete_values(*args) args.each_with_object([]) { |val, array| array << delete(val) } end |
#demote(value) ⇒ Object
134 135 136 |
# File 'lib/lite/ruby/array.rb', line 134 def demote(value) sort_by { |val| val == value ? 0 : -1 } end |
#demote!(value) ⇒ Object
138 139 140 |
# File 'lib/lite/ruby/array.rb', line 138 def demote!(value) replace(demote(value)) end |
#denillify(identity = 0) ⇒ Object
142 143 144 |
# File 'lib/lite/ruby/array.rb', line 142 def denillify(identity = 0) map { |val| val || identity } end |
#denillify!(identity = 0) ⇒ Object
146 147 148 |
# File 'lib/lite/ruby/array.rb', line 146 def denillify!(identity = 0) replace(denillify(identity)) end |
#divergence(other) ⇒ Object
150 151 152 |
# File 'lib/lite/ruby/array.rb', line 150 def divergence(other) (self - other) | (other - self) end |
#duplicates(minimum = 2) ⇒ Object
154 155 156 157 158 |
# File 'lib/lite/ruby/array.rb', line 154 def duplicates(minimum = 2) hash = Hash.new(0) each { |val| hash[val] += 1 } hash.delete_if { |_, val| val < minimum }.keys end |
#except(*values) ⇒ Object Also known as: reject_values
160 161 162 |
# File 'lib/lite/ruby/array.rb', line 160 def except(*values) reject { |val| values.include?(val) } end |
#except!(*values) ⇒ Object Also known as: reject_values!
164 165 166 167 |
# File 'lib/lite/ruby/array.rb', line 164 def except!(*values) reject! { |val| values.include?(val) } self end |
#extract! ⇒ Object
9 10 11 12 13 14 15 |
# File 'lib/lite/ruby/safe/array.rb', line 9 def extract! return to_enum(:extract!) { size } unless block_given? extracted_elements = [] reject! { |element| extracted_elements << element if yield(element) } extracted_elements end |
#from(position) ⇒ Object
17 18 19 |
# File 'lib/lite/ruby/safe/array.rb', line 17 def from(position) self[position, size] || [] end |
#fulfill(value, amount) ⇒ Object
169 170 171 172 173 |
# File 'lib/lite/ruby/array.rb', line 169 def fulfill(value, amount) return self if amount <= size fill(value, size..(amount - 1)) end |
#groups(number) ⇒ Object
175 176 177 178 179 180 181 182 183 |
# File 'lib/lite/ruby/array.rb', line 175 def groups(number) return [] if number <= 0 num, rem = size.divmod(number) collection = (0..(num - 1)).collect { |val| self[(val * number), number] } return collection unless rem.positive? collection << self[-rem, rem] end |
#in_groups(number, fill_with = nil, &block) ⇒ Object
rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/MethodLength
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
# File 'lib/lite/ruby/safe/array.rb', line 22 def in_groups(number, fill_with = nil, &block) collection_size = size division = collection_size.div(number) modulo = collection_size % number collection = [] start = 0 number.times do |int| mod_gt_zero = modulo.positive? grouping = division + (mod_gt_zero && modulo > int ? 1 : 0) collection << last_group = slice(start, grouping) last_group << fill_with if (fill_with != false) && mod_gt_zero && (grouping == division) start += grouping end return collection unless defined?(yield) collection.each(&block) end |
#in_groups_of(number, fill_with = nil, &block) ⇒ Object
rubocop:disable Metrics/MethodLength, Style/GuardClause
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
# File 'lib/lite/ruby/safe/array.rb', line 44 def in_groups_of(number, fill_with = nil, &block) if number.to_i <= 0 raise ArgumentError, "Group size must be a positive integer, was #{number.inspect}" elsif fill_with == false collection = self else padding = (number - size % number) % number collection = dup.concat(Array.new(padding, fill_with)) end sliced_collection = collection.each_slice(number) return sliced_collection.to_a unless defined?(yield) sliced_collection(&block) end |
#indexes(value) ⇒ Object Also known as: indices
185 186 187 188 189 |
# File 'lib/lite/ruby/array.rb', line 185 def indexes(value) array = [] each_with_index { |val, i| array << i if value == val } array end |
#merge(*values) ⇒ Object
191 192 193 |
# File 'lib/lite/ruby/array.rb', line 191 def merge(*values) dup.merge!(*values) end |
#merge!(*values) ⇒ Object
195 196 197 |
# File 'lib/lite/ruby/array.rb', line 195 def merge!(*values) values.each_with_object(self) { |val, arr| arr.concat(val) } end |
#nillify ⇒ Object
199 200 201 |
# File 'lib/lite/ruby/array.rb', line 199 def nillify map { |val| !val.nil? && (val.try(:blank?) || val.try(:to_s).blank?) ? nil : val } end |
#nillify! ⇒ Object
203 204 205 |
# File 'lib/lite/ruby/array.rb', line 203 def nillify! replace(nillify) end |
#only(*values) ⇒ Object Also known as: select_values
207 208 209 |
# File 'lib/lite/ruby/array.rb', line 207 def only(*values) select { |val| values.include?(val) } end |
#only!(*values) ⇒ Object Also known as: select_values!
211 212 213 214 |
# File 'lib/lite/ruby/array.rb', line 211 def only!(*values) select! { |val| values.include?(val) } self end |
#position(value) ⇒ Object
216 217 218 219 220 221 |
# File 'lib/lite/ruby/array.rb', line 216 def position(value) idx = index(value) return idx if idx.nil? idx + 1 end |
#positions(value) ⇒ Object
223 224 225 |
# File 'lib/lite/ruby/array.rb', line 223 def positions(value) indexes(value).map { |val| val + 1 } end |
#probability ⇒ Object
227 228 229 230 231 232 233 234 235 236 237 238 |
# File 'lib/lite/ruby/array.rb', line 227 def probability hash = Hash.new(0.0) differ = 0.0 each do |val| hash[val] += 1.0 differ += 1.0 end hash.each_key { |val| hash[val] /= differ } hash end |
#promote(value) ⇒ Object
240 241 242 |
# File 'lib/lite/ruby/array.rb', line 240 def promote(value) sort_by { |val| val == value ? -1 : 0 } end |
#promote!(value) ⇒ Object
244 245 246 |
# File 'lib/lite/ruby/array.rb', line 244 def promote!(value) sort_by! { |val| val == value ? -1 : 0 } end |
#rand_sample(max = nil) ⇒ Object
248 249 250 251 |
# File 'lib/lite/ruby/array.rb', line 248 def rand_sample(max = nil) amount = rand(1..(max || size)) sample(amount) end |
#rposition(value) ⇒ Object
253 254 255 256 257 258 |
# File 'lib/lite/ruby/array.rb', line 253 def rposition(value) idx = rindex(value) return idx if idx.nil? idx + 1 end |
#sample! ⇒ Object
260 261 262 |
# File 'lib/lite/ruby/array.rb', line 260 def sample! delete_at(Random.rand(size - 1)) end |
#split(value = nil) ⇒ Object
rubocop:disable Metrics/MethodLength, Style/ExplicitBlockArgument
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 |
# File 'lib/lite/ruby/safe/array.rb', line 62 def split(value = nil) arr = dup result = [] if block_given? while (idx = arr.index { |i| yield(i) }) result << arr.shift(idx) arr.shift end else while (idx = arr.index(value)) result << arr.shift(idx) arr.shift end end result << arr end |
#strip ⇒ Object
264 265 266 |
# File 'lib/lite/ruby/array.rb', line 264 def strip reject(&:blank?) end |
#strip! ⇒ Object
268 269 270 271 |
# File 'lib/lite/ruby/array.rb', line 268 def strip! reject!(&:blank?) self end |
#swap(from, to) ⇒ Object
273 274 275 276 |
# File 'lib/lite/ruby/array.rb', line 273 def swap(from, to) self[from], self[to] = self[to], self[from] self end |
#to(position) ⇒ Object
rubocop:enable Metrics/MethodLength, Style/ExplicitBlockArgument
82 83 84 85 86 |
# File 'lib/lite/ruby/safe/array.rb', line 82 def to(position) return first(position + 1) if position >= 0 self[0..position] end |
#to_sentence(options = {}) ⇒ Object
88 89 90 91 92 93 94 95 96 97 98 99 |
# File 'lib/lite/ruby/safe/array.rb', line 88 def to_sentence( = {}) words_connector = [:words_connector] || ', ' two_words_connector = [:two_words_connector] || ' and ' last_word_connector = [:last_word_connector] || ', and ' case size when 0 then '' when 1 then self[0].to_s when 2 then "#{self[0]}#{two_words_connector}#{self[1]}" else "#{self[0...-1].join(words_connector)}#{last_word_connector}#{self[-1]}" end end |