Class: Array

Inherits:
Object show all
Defined in:
lib/lite/ruby/array.rb

Instance Method Summary collapse

Instance Method Details

#after(value) ⇒ Object



22
23
24
25
26
# File 'lib/lite/ruby/array.rb', line 22

def after(value)
  return unless include?(value)

  self[(index(value) + 1) % size]
end

#assert_all_valid_values!(*valid_values) ⇒ Object

Raises:

  • (ArgumentError)


16
17
18
19
20
# File 'lib/lite/ruby/array.rb', line 16

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



6
7
8
9
10
11
12
13
14
# File 'lib/lite/ruby/array.rb', line 6

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



28
29
30
31
32
# File 'lib/lite/ruby/array.rb', line 28

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



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/lite/ruby/array.rb', line 36

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_dupObject

rubocop:enable Metrics/PerceivedComplexity, Style/GuardClause, Style/IfInsideElse rubocop:enable Metrics/AbcSize, Metrics/BlockNesting, Metrics/MethodLength



60
61
62
# File 'lib/lite/ruby/array.rb', line 60

def deep_dup
  map(&:deep_dup)
end

#delete_firstObject



64
65
66
# File 'lib/lite/ruby/array.rb', line 64

def delete_first
  self[1..-1]
end

#delete_first!Object



68
69
70
# File 'lib/lite/ruby/array.rb', line 68

def delete_first!
  replace(delete_first)
end

#delete_lastObject



72
73
74
# File 'lib/lite/ruby/array.rb', line 72

def delete_last
  self[0...-1]
end

#delete_last!Object



76
77
78
# File 'lib/lite/ruby/array.rb', line 76

def delete_last!
  replace(delete_last)
end

#delete_values(*args) ⇒ Object



80
81
82
# File 'lib/lite/ruby/array.rb', line 80

def delete_values(*args)
  args.each_with_object([]) { |val, array| array << delete(val) }
end

#demote(value) ⇒ Object



84
85
86
# File 'lib/lite/ruby/array.rb', line 84

def demote(value)
  sort_by { |val| val == value ? 0 : -1 }
end

#demote!(value) ⇒ Object



88
89
90
# File 'lib/lite/ruby/array.rb', line 88

def demote!(value)
  replace(demote(value))
end

#denillify(identity = 0) ⇒ Object



92
93
94
# File 'lib/lite/ruby/array.rb', line 92

def denillify(identity = 0)
  map { |val| val || identity }
end

#denillify!(identity = 0) ⇒ Object



96
97
98
# File 'lib/lite/ruby/array.rb', line 96

def denillify!(identity = 0)
  replace(denillify(identity))
end

#duplicates(minimum = 2) ⇒ Object



100
101
102
103
104
# File 'lib/lite/ruby/array.rb', line 100

def duplicates(minimum = 2)
  hash = Hash.new(0)
  each { |val| hash[val] += 1 }
  hash.delete_if { |_, val| val < minimum }.keys
end

#from(position) ⇒ Object



106
107
108
# File 'lib/lite/ruby/array.rb', line 106

def from(position)
  self[position, size] || []
end

#fulfill(value, amount) ⇒ Object



110
111
112
113
114
# File 'lib/lite/ruby/array.rb', line 110

def fulfill(value, amount)
  return self if amount <= size

  fill(value, size..(amount - 1))
end

#groups(number) ⇒ Object



116
117
118
119
120
121
122
123
124
# File 'lib/lite/ruby/array.rb', line 116

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



127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
# File 'lib/lite/ruby/array.rb', line 127

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



149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
# File 'lib/lite/ruby/array.rb', line 149

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



166
167
168
169
170
# File 'lib/lite/ruby/array.rb', line 166

def indexes(value)
  array = []
  each_with_index { |val, i| array << i if value == val }
  array
end

#merge(*values) ⇒ Object



174
175
176
# File 'lib/lite/ruby/array.rb', line 174

def merge(*values)
  dup.merge!(*values)
end

#merge!(*values) ⇒ Object



178
179
180
# File 'lib/lite/ruby/array.rb', line 178

def merge!(*values)
  values.each_with_object(self) { |val, arr| arr.concat(val) }
end

#nillifyObject



182
183
184
# File 'lib/lite/ruby/array.rb', line 182

def nillify
  map { |val| !val.nil? && (val.try(:blank?) || val.try(:to_s).blank?) ? nil : val }
end

#nillify!Object



186
187
188
# File 'lib/lite/ruby/array.rb', line 186

def nillify!
  replace(nillify)
end

#position(value) ⇒ Object



190
191
192
193
194
195
# File 'lib/lite/ruby/array.rb', line 190

def position(value)
  idx = index(value)
  return idx if idx.nil?

  idx + 1
end

#positions(value) ⇒ Object



197
198
199
# File 'lib/lite/ruby/array.rb', line 197

def positions(value)
  indexes(value).map { |val| val + 1 }
end

#probabilityObject



201
202
203
204
205
206
207
208
209
210
211
212
# File 'lib/lite/ruby/array.rb', line 201

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



214
215
216
# File 'lib/lite/ruby/array.rb', line 214

def promote(value)
  sort_by { |val| val == value ? -1 : 0 }
end

#promote!(value) ⇒ Object



218
219
220
# File 'lib/lite/ruby/array.rb', line 218

def promote!(value)
  replace(promote(value))
end

#reject_values(*args) ⇒ Object



222
223
224
# File 'lib/lite/ruby/array.rb', line 222

def reject_values(*args)
  reject { |val| args.include?(val) }
end

#rposition(value) ⇒ Object



226
227
228
229
230
231
# File 'lib/lite/ruby/array.rb', line 226

def rposition(value)
  idx = rindex(value)
  return idx if idx.nil?

  idx + 1
end

#sample!Object



233
234
235
# File 'lib/lite/ruby/array.rb', line 233

def sample!
  delete_at(Random.rand(size - 1))
end

#split(number = nil) ⇒ Object

rubocop:disable Metrics/AbcSize, Metrics/BlockNesting, Metrics/MethodLength



238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
# File 'lib/lite/ruby/array.rb', line 238

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

#stripObject

rubocop:enable Metrics/AbcSize, Metrics/BlockNesting, Metrics/MethodLength



261
262
263
# File 'lib/lite/ruby/array.rb', line 261

def strip
  reject(&:blank?)
end

#strip!Object



265
266
267
# File 'lib/lite/ruby/array.rb', line 265

def strip!
  replace(strip)
end

#swap(from, to) ⇒ Object



269
270
271
272
# File 'lib/lite/ruby/array.rb', line 269

def swap(from, to)
  self[from], self[to] = self[to], self[from]
  self
end

#to(position) ⇒ Object



274
275
276
277
278
# File 'lib/lite/ruby/array.rb', line 274

def to(position)
  return first(position + 1) if position >= 0

  self[0..position]
end

#to_sentence(options = {}) ⇒ Object

rubocop:disable Metrics/CyclomaticComplexity



281
282
283
284
285
286
287
288
289
290
291
292
# File 'lib/lite/ruby/array.rb', line 281

def to_sentence(options = {})
  words_connector = options[:words_connector] || ', '
  two_words_connector = options[:two_words_connector] || ' and '
  last_word_connector = options[: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