Class: Array
Instance Method Summary collapse
- #after(value) ⇒ Object
- #assert_all_valid_values!(*valid_values) ⇒ Object
- #assert_valid_values!(*valid_values) ⇒ Object
- #before(value) ⇒ Object
-
#bury(*args) ⇒ Object
rubocop:disable Metrics/AbcSize, Metrics/MethodLength rubocop:disable Metrics/PerceivedComplexity, Style/GuardClause, Style/IfInsideElse.
-
#delete_first ⇒ Object
rubocop:enable Metrics/PerceivedComplexity, Style/GuardClause, Style/IfInsideElse rubocop:enable Metrics/AbcSize, 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
- #duplicates(minimum = 2) ⇒ Object
- #from(position) ⇒ Object
- #fulfill(value, amount) ⇒ Object
- #groups(number) ⇒ Object
-
#in_groups(number, fill_with = nil) ⇒ Object
rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/MethodLength.
-
#in_groups_of(number, fill_with = nil) ⇒ Object
rubocop:disable Metrics/AbcSize, Metrics/MethodLength, Style/GuardClause.
-
#indexes(value) ⇒ Object
(also: #indices)
rubocop:enable Metrics/AbcSize, Metrics/MethodLength, Style/GuardClause.
- #merge(*values) ⇒ Object
- #merge!(*values) ⇒ Object
- #nillify ⇒ Object
- #nillify! ⇒ Object
- #position(value) ⇒ Object
- #positions(value) ⇒ Object
- #probability ⇒ Object
- #promote(value) ⇒ Object
- #promote!(value) ⇒ Object
- #reject_values(*args) ⇒ Object
- #rposition(value) ⇒ Object
- #sample! ⇒ Object
-
#split(number = nil) ⇒ Object
rubocop:disable Metrics/AbcSize, Metrics/MethodLength.
-
#strip ⇒ Object
rubocop:enable Metrics/AbcSize, Metrics/MethodLength.
- #strip! ⇒ Object
- #swap(from, to) ⇒ Object
- #to(position) ⇒ Object
-
#to_sentence(options = {}) ⇒ Object
rubocop:disable Metrics/CyclomaticComplexity.
Instance Method Details
#after(value) ⇒ Object
21 22 23 24 25 |
# File 'lib/lite/ruby/array.rb', line 21 def after(value) return unless include?(value) self[(index(value) + 1) % size] end |
#assert_all_valid_values!(*valid_values) ⇒ Object
15 16 17 18 19 |
# File 'lib/lite/ruby/array.rb', line 15 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_valid_values!(*valid_values) ⇒ Object
5 6 7 8 9 10 11 12 13 |
# File 'lib/lite/ruby/array.rb', line 5 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 |
#before(value) ⇒ Object
27 28 29 30 31 |
# File 'lib/lite/ruby/array.rb', line 27 def before(value) return unless include?(value) self[(index(value) - 1) % size] end |
#bury(*args) ⇒ Object
rubocop:disable Metrics/AbcSize, Metrics/MethodLength rubocop:disable Metrics/PerceivedComplexity, Style/GuardClause, Style/IfInsideElse
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
# File 'lib/lite/ruby/array.rb', line 35 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 |
#delete_first ⇒ Object
rubocop:enable Metrics/PerceivedComplexity, Style/GuardClause, Style/IfInsideElse rubocop:enable Metrics/AbcSize, Metrics/MethodLength
59 60 61 |
# File 'lib/lite/ruby/array.rb', line 59 def delete_first self[1..-1] end |
#delete_first! ⇒ Object
63 64 65 |
# File 'lib/lite/ruby/array.rb', line 63 def delete_first! replace(delete_first) end |
#delete_last ⇒ Object
67 68 69 |
# File 'lib/lite/ruby/array.rb', line 67 def delete_last self[0...-1] end |
#delete_last! ⇒ Object
71 72 73 |
# File 'lib/lite/ruby/array.rb', line 71 def delete_last! replace(delete_last) end |
#delete_values(*args) ⇒ Object
75 76 77 |
# File 'lib/lite/ruby/array.rb', line 75 def delete_values(*args) args.each_with_object([]) { |val, array| array << delete(val) } end |
#demote(value) ⇒ Object
79 80 81 |
# File 'lib/lite/ruby/array.rb', line 79 def demote(value) sort_by { |val| val == value ? 0 : -1 } end |
#demote!(value) ⇒ Object
83 84 85 |
# File 'lib/lite/ruby/array.rb', line 83 def demote!(value) replace(demote(value)) end |
#denillify(identity = 0) ⇒ Object
87 88 89 |
# File 'lib/lite/ruby/array.rb', line 87 def denillify(identity = 0) map { |val| val || identity } end |
#denillify!(identity = 0) ⇒ Object
91 92 93 |
# File 'lib/lite/ruby/array.rb', line 91 def denillify!(identity = 0) replace(denillify(identity)) end |
#duplicates(minimum = 2) ⇒ Object
95 96 97 98 99 |
# File 'lib/lite/ruby/array.rb', line 95 def duplicates(minimum = 2) hash = Hash.new(0) each { |val| hash[val] += 1 } hash.delete_if { |_, val| val < minimum }.keys end |
#from(position) ⇒ Object
101 102 103 |
# File 'lib/lite/ruby/array.rb', line 101 def from(position) self[position, size] || [] end |
#fulfill(value, amount) ⇒ Object
105 106 107 108 109 |
# File 'lib/lite/ruby/array.rb', line 105 def fulfill(value, amount) return self if amount <= size fill(value, size..(amount - 1)) end |
#groups(number) ⇒ Object
111 112 113 114 115 116 117 118 119 |
# File 'lib/lite/ruby/array.rb', line 111 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) ⇒ Object
rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/MethodLength
122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 |
# File 'lib/lite/ruby/array.rb', line 122 def in_groups(number, fill_with = nil) 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 block_given? collection.each { |val| yield(val) } end |
#in_groups_of(number, fill_with = nil) ⇒ Object
rubocop:disable Metrics/AbcSize, Metrics/MethodLength, Style/GuardClause
144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 |
# File 'lib/lite/ruby/array.rb', line 144 def in_groups_of(number, fill_with = nil) 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 block_given? sliced_collection { |val| yield(val) } end |
#indexes(value) ⇒ Object Also known as: indices
rubocop:enable Metrics/AbcSize, Metrics/MethodLength, Style/GuardClause
161 162 163 164 165 |
# File 'lib/lite/ruby/array.rb', line 161 def indexes(value) array = [] each_with_index { |val, i| array << i if value == val } array end |
#merge(*values) ⇒ Object
169 170 171 |
# File 'lib/lite/ruby/array.rb', line 169 def merge(*values) dup.merge!(*values) end |
#merge!(*values) ⇒ Object
173 174 175 |
# File 'lib/lite/ruby/array.rb', line 173 def merge!(*values) values.each_with_object(self) { |val, arr| arr.concat(val) } end |
#nillify ⇒ Object
177 178 179 |
# File 'lib/lite/ruby/array.rb', line 177 def nillify map { |val| !val.nil? && (val.try(:blank?) || val.try(:to_s).blank?) ? nil : val } end |
#nillify! ⇒ Object
181 182 183 |
# File 'lib/lite/ruby/array.rb', line 181 def nillify! replace(nillify) end |
#position(value) ⇒ Object
185 186 187 188 189 190 |
# File 'lib/lite/ruby/array.rb', line 185 def position(value) idx = index(value) return idx if idx.nil? idx + 1 end |
#positions(value) ⇒ Object
192 193 194 |
# File 'lib/lite/ruby/array.rb', line 192 def positions(value) indexes(value).map { |val| val + 1 } end |
#probability ⇒ Object
196 197 198 199 200 201 202 203 204 205 206 207 |
# File 'lib/lite/ruby/array.rb', line 196 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
209 210 211 |
# File 'lib/lite/ruby/array.rb', line 209 def promote(value) sort_by { |val| val == value ? -1 : 0 } end |
#promote!(value) ⇒ Object
213 214 215 |
# File 'lib/lite/ruby/array.rb', line 213 def promote!(value) replace(promote(value)) end |
#reject_values(*args) ⇒ Object
217 218 219 |
# File 'lib/lite/ruby/array.rb', line 217 def reject_values(*args) reject { |val| args.include?(val) } end |
#rposition(value) ⇒ Object
221 222 223 224 225 226 |
# File 'lib/lite/ruby/array.rb', line 221 def rposition(value) idx = rindex(value) return idx if idx.nil? idx + 1 end |
#sample! ⇒ Object
228 229 230 |
# File 'lib/lite/ruby/array.rb', line 228 def sample! delete_at(Random.rand(size - 1)) end |
#split(number = nil) ⇒ Object
rubocop:disable Metrics/AbcSize, Metrics/MethodLength
233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 |
# File 'lib/lite/ruby/array.rb', line 233 def split(number = nil) array = [[]] if block_given? 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/MethodLength
256 257 258 |
# File 'lib/lite/ruby/array.rb', line 256 def strip reject(&:blank?) end |
#strip! ⇒ Object
260 261 262 |
# File 'lib/lite/ruby/array.rb', line 260 def strip! replace(strip) end |
#swap(from, to) ⇒ Object
264 265 266 267 |
# File 'lib/lite/ruby/array.rb', line 264 def swap(from, to) self[from], self[to] = self[to], self[from] self end |
#to(position) ⇒ Object
269 270 271 272 273 |
# File 'lib/lite/ruby/array.rb', line 269 def to(position) return first(position + 1) if position >= 0 self[0..position] end |
#to_sentence(options = {}) ⇒ Object
rubocop:disable Metrics/CyclomaticComplexity
276 277 278 279 280 281 282 283 284 285 286 287 |
# File 'lib/lite/ruby/array.rb', line 276 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 |