Class: Array
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
rubocop:enable Metrics/PerceivedComplexity, Style/GuardClause, Style/IfInsideElse rubocop:enable Metrics/AbcSize, Metrics/BlockNesting, Metrics/MethodLength.
- #delete_first ⇒ Object
- #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
- #duplicates(minimum = 2) ⇒ Object
- #except(*values) ⇒ Object
- #except!(*values) ⇒ 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)
rubocop:enable Metrics/MethodLength, Style/GuardClause.
- #merge(*values) ⇒ Object
- #merge!(*values) ⇒ Object
- #nillify ⇒ Object
- #nillify! ⇒ Object
- #only(*values) ⇒ Object
- #only!(*values) ⇒ Object
- #position(value) ⇒ Object
- #positions(value) ⇒ Object
- #probability ⇒ Object
- #promote(value) ⇒ Object
- #promote!(value) ⇒ Object
- #rand_sample(max = nil) ⇒ Object
- #reject_values(*args) ⇒ Object
- #rposition(value) ⇒ Object
- #sample! ⇒ Object
-
#split(number = nil) ⇒ Object
rubocop:disable Metrics/AbcSize, Metrics/BlockNesting, Metrics/MethodLength.
-
#strip ⇒ Object
rubocop:enable Metrics/AbcSize, Metrics/BlockNesting, Metrics/MethodLength.
- #strip! ⇒ Object
- #swap(from, to) ⇒ Object
- #to(position) ⇒ Object
- #to_sentence(options = {}) ⇒ Object
Instance Method Details
#after(value) ⇒ Object
56 57 58 59 60 |
# File 'lib/lite/ruby/array.rb', line 56 def after(value) return unless include?(value) self[(index(value) + 1) % size] end |
#all_after(value) ⇒ Object
62 63 64 65 66 67 68 69 |
# File 'lib/lite/ruby/array.rb', line 62 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
71 72 73 74 75 76 77 78 |
# File 'lib/lite/ruby/array.rb', line 71 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
20 21 22 23 24 |
# File 'lib/lite/ruby/array.rb', line 20 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
36 37 38 39 40 |
# File 'lib/lite/ruby/array.rb', line 36 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
50 51 52 53 54 |
# File 'lib/lite/ruby/array.rb', line 50 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
6 7 8 9 10 11 12 13 14 15 16 17 18 |
# File 'lib/lite/ruby/array.rb', line 6 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
26 27 28 29 30 31 32 33 34 |
# File 'lib/lite/ruby/array.rb', line 26 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
42 43 44 45 46 47 48 |
# File 'lib/lite/ruby/array.rb', line 42 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
80 81 82 83 84 |
# File 'lib/lite/ruby/array.rb', line 80 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
88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 |
# File 'lib/lite/ruby/array.rb', line 88 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
rubocop:enable Metrics/PerceivedComplexity, Style/GuardClause, Style/IfInsideElse rubocop:enable Metrics/AbcSize, Metrics/BlockNesting, Metrics/MethodLength
112 113 114 |
# File 'lib/lite/ruby/array.rb', line 112 def deep_dup map(&:deep_dup) end |
#delete_first ⇒ Object
116 117 118 |
# File 'lib/lite/ruby/array.rb', line 116 def delete_first self[1..-1] end |
#delete_first! ⇒ Object
120 121 122 |
# File 'lib/lite/ruby/array.rb', line 120 def delete_first! replace(delete_first) end |
#delete_last ⇒ Object
124 125 126 |
# File 'lib/lite/ruby/array.rb', line 124 def delete_last self[0...-1] end |
#delete_last! ⇒ Object
128 129 130 |
# File 'lib/lite/ruby/array.rb', line 128 def delete_last! replace(delete_last) end |
#delete_values(*args) ⇒ Object
132 133 134 |
# File 'lib/lite/ruby/array.rb', line 132 def delete_values(*args) args.each_with_object([]) { |val, array| array << delete(val) } end |
#demote(value) ⇒ Object
136 137 138 |
# File 'lib/lite/ruby/array.rb', line 136 def demote(value) sort_by { |val| val == value ? 0 : -1 } end |
#demote!(value) ⇒ Object
140 141 142 |
# File 'lib/lite/ruby/array.rb', line 140 def demote!(value) replace(demote(value)) end |
#denillify(identity = 0) ⇒ Object
144 145 146 |
# File 'lib/lite/ruby/array.rb', line 144 def denillify(identity = 0) map { |val| val || identity } end |
#denillify!(identity = 0) ⇒ Object
148 149 150 |
# File 'lib/lite/ruby/array.rb', line 148 def denillify!(identity = 0) replace(denillify(identity)) end |
#duplicates(minimum = 2) ⇒ Object
152 153 154 155 156 |
# File 'lib/lite/ruby/array.rb', line 152 def duplicates(minimum = 2) hash = Hash.new(0) each { |val| hash[val] += 1 } hash.delete_if { |_, val| val < minimum }.keys end |
#except(*values) ⇒ Object
158 159 160 |
# File 'lib/lite/ruby/array.rb', line 158 def except(*values) reject { |val| values.include?(val) } end |
#except!(*values) ⇒ Object
162 163 164 |
# File 'lib/lite/ruby/array.rb', line 162 def except!(*values) replace(except(*values)) end |
#from(position) ⇒ Object
166 167 168 |
# File 'lib/lite/ruby/array.rb', line 166 def from(position) self[position, size] || [] end |
#fulfill(value, amount) ⇒ Object
170 171 172 173 174 |
# File 'lib/lite/ruby/array.rb', line 170 def fulfill(value, amount) return self if amount <= size fill(value, size..(amount - 1)) end |
#groups(number) ⇒ Object
176 177 178 179 180 181 182 183 184 |
# File 'lib/lite/ruby/array.rb', line 176 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
187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 |
# File 'lib/lite/ruby/array.rb', line 187 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
209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 |
# File 'lib/lite/ruby/array.rb', line 209 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
rubocop:enable Metrics/MethodLength, Style/GuardClause
226 227 228 229 230 |
# File 'lib/lite/ruby/array.rb', line 226 def indexes(value) array = [] each_with_index { |val, i| array << i if value == val } array end |
#merge(*values) ⇒ Object
234 235 236 |
# File 'lib/lite/ruby/array.rb', line 234 def merge(*values) dup.merge!(*values) end |
#merge!(*values) ⇒ Object
238 239 240 |
# File 'lib/lite/ruby/array.rb', line 238 def merge!(*values) values.each_with_object(self) { |val, arr| arr.concat(val) } end |
#nillify ⇒ Object
242 243 244 |
# File 'lib/lite/ruby/array.rb', line 242 def nillify map { |val| !val.nil? && (val.try(:blank?) || val.try(:to_s).blank?) ? nil : val } end |
#nillify! ⇒ Object
246 247 248 |
# File 'lib/lite/ruby/array.rb', line 246 def nillify! replace(nillify) end |
#only(*values) ⇒ Object
250 251 252 |
# File 'lib/lite/ruby/array.rb', line 250 def only(*values) select { |val| values.include?(val) } end |
#only!(*values) ⇒ Object
254 255 256 |
# File 'lib/lite/ruby/array.rb', line 254 def only!(*values) replace(only(*values)) end |
#position(value) ⇒ Object
258 259 260 261 262 263 |
# File 'lib/lite/ruby/array.rb', line 258 def position(value) idx = index(value) return idx if idx.nil? idx + 1 end |
#positions(value) ⇒ Object
265 266 267 |
# File 'lib/lite/ruby/array.rb', line 265 def positions(value) indexes(value).map { |val| val + 1 } end |
#probability ⇒ Object
269 270 271 272 273 274 275 276 277 278 279 280 |
# File 'lib/lite/ruby/array.rb', line 269 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
282 283 284 |
# File 'lib/lite/ruby/array.rb', line 282 def promote(value) sort_by { |val| val == value ? -1 : 0 } end |
#promote!(value) ⇒ Object
286 287 288 |
# File 'lib/lite/ruby/array.rb', line 286 def promote!(value) replace(promote(value)) end |
#rand_sample(max = nil) ⇒ Object
290 291 292 293 |
# File 'lib/lite/ruby/array.rb', line 290 def rand_sample(max = nil) amount = rand(1..(max || size)) sample(amount) end |
#reject_values(*args) ⇒ Object
295 296 297 |
# File 'lib/lite/ruby/array.rb', line 295 def reject_values(*args) reject { |val| args.include?(val) } end |
#rposition(value) ⇒ Object
299 300 301 302 303 304 |
# File 'lib/lite/ruby/array.rb', line 299 def rposition(value) idx = rindex(value) return idx if idx.nil? idx + 1 end |
#sample! ⇒ Object
306 307 308 |
# File 'lib/lite/ruby/array.rb', line 306 def sample! delete_at(Random.rand(size - 1)) end |
#split(number = nil) ⇒ Object
rubocop:disable Metrics/AbcSize, Metrics/BlockNesting, Metrics/MethodLength
311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 |
# File 'lib/lite/ruby/array.rb', line 311 def split(number = nil) array = [[]] if defined?(yield) each { |val| yield(val) ? (array << []) : (array.last << val) } else dup_arr = dup until dup_arr.empty? if (idx = dup_arr.index(number)) array.last << dup_arr.shift(idx) dup_arr.shift array << [] else array.last << arr.shift(dup_arr.size) end end end array end |
#strip ⇒ Object
rubocop:enable Metrics/AbcSize, Metrics/BlockNesting, Metrics/MethodLength
334 335 336 |
# File 'lib/lite/ruby/array.rb', line 334 def strip reject(&:blank?) end |
#strip! ⇒ Object
338 339 340 |
# File 'lib/lite/ruby/array.rb', line 338 def strip! replace(strip) end |
#swap(from, to) ⇒ Object
342 343 344 345 |
# File 'lib/lite/ruby/array.rb', line 342 def swap(from, to) self[from], self[to] = self[to], self[from] self end |
#to(position) ⇒ Object
347 348 349 350 351 |
# File 'lib/lite/ruby/array.rb', line 347 def to(position) return first(position + 1) if position >= 0 self[0..position] end |
#to_sentence(options = {}) ⇒ Object
353 354 355 356 357 358 359 360 361 362 363 364 |
# File 'lib/lite/ruby/array.rb', line 353 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.dup when 2 then "#{self[0]}#{two_words_connector}#{self[1]}" else "#{self[0...-1].join(words_connector)}#{last_word_connector}#{self[-1]}" end end |