Class: Array

Inherits:
Object show all
Defined in:
lib/ww.rb,
lib/ww.rb,
lib/array.rb

Instance Method Summary collapse

Instance Method Details

#count_by(method) ⇒ Hash

Count occurrences by method result using tally (Ruby 2.7+)

Parameters:

  • method (Symbol, String)

    The method to call on each element

Returns:

  • (Hash)

    Hash of method result => count pairs



273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
# File 'lib/ww.rb', line 273

def count_by(method)
  raise ArgumentError,
        'Method must be a Symbol or String' unless method.is_a?(Symbol) || method.is_a?(String)

  if respond_to?(:tally) # Ruby 2.7+
    map do |item|
      item.respond_to?(method) ? item.send(method) : nil
    end.compact.tally
  else
    # Fallback for older Ruby versions
    result = Hash.new(0)
    each do |item|
      if item.respond_to?(method)
        value = item.send(method)
        result[value] += 1
      end
    end
    result
  end
rescue NoMethodError => err
  warn "Method #{method} not available on some items: #{err.message}"
  {}
end

#deref(count = 4) ⇒ Object

trim the backtrace to project source files exclude vendor and .bundle directories limit the count to 4 replace the home directory with a .



248
249
250
251
252
253
254
255
# File 'lib/ww.rb', line 248

def deref(count = 4)
  dir_pwd = Dir.pwd
  map(&:deref).reject do |line|
    %r{^/(vendor|\.bundle)/}.match(line)
  end.first(count).map do |line|
    line.sub(dir_pwd, '.')
  end
end

#filter_map_by(method, value, transform_method = nil) ⇒ Array

Use filter_map for combined filtering and mapping (Ruby 2.7+) Filters elements and transforms them in one pass

Parameters:

  • method (Symbol, String)

    The method to call on each element

  • value (Object)

    The value to match against

  • transform_method (Symbol, String, nil) (defaults to: nil)

    Optional method to call on matching elements

Returns:

  • (Array)

    Array of transformed matching elements



305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
# File 'lib/ww.rb', line 305

def filter_map_by(method, value, transform_method = nil)
  raise ArgumentError,
        'Method must be a Symbol or String' unless method.is_a?(Symbol) || method.is_a?(String)

  if respond_to?(:filter_map) # Ruby 2.7+
    filter_map do |item|
      if item.respond_to?(method) && item.send(method) == value
        transform_method ? item.send(transform_method) : item
      end
    end
  else
    # Fallback for older Ruby versions
    result = []
    each do |item|
      if item.respond_to?(method) && item.send(method) == value
        result << (transform_method ? item.send(transform_method) : item)
      end
    end
    result
  end
rescue NoMethodError => err
  warn "Method #{method} not available on some items: #{err.message}"
  []
end

#find_by(method = nil, value = nil, default = nil, &block) ⇒ Object?

Find the first element where the specified method matches a value Supports both method-based and block-based filtering

Parameters:

  • method (Symbol, String, nil) (defaults to: nil)

    The method to call on each element

  • value (Object, nil) (defaults to: nil)

    The value to match against

  • default (Object, nil) (defaults to: nil)

    Default value if no match found

  • block (Proc, nil)

    Optional block for custom filtering logic

Returns:

  • (Object, nil)

    The first matching element or default



338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
# File 'lib/ww.rb', line 338

def find_by(method = nil, value = nil, default = nil, &block)
  if block_given?
    find(&block)
  else
    raise ArgumentError,
          'Method must be a Symbol or String' unless method.is_a?(Symbol) || method.is_a?(String)

    find do |item|
      item.respond_to?(method) && item.send(method) == value
    end || default
  end
rescue NoMethodError => err
  warn "Method #{method} not available on some items: #{err.message}"
  default
end

#find_where(conditions = {}) ⇒ Array

Find elements using hash-based conditions All conditions must match for an element to be included

Parameters:

  • conditions (Hash) (defaults to: {})

    Hash of method => value pairs

Returns:

  • (Array)

    Array of matching elements



359
360
361
362
363
364
365
366
367
368
# File 'lib/ww.rb', line 359

def find_where(conditions = {})
  find do |item|
    conditions.all? do |method, value|
      item.respond_to?(method) && item.send(method) == value
    end
  end
rescue NoMethodError => err
  warn "Some methods not available on items: #{err.message}"
  nil
end

#match_by(method, pattern) ⇒ Array

Match elements using pattern matching (Ruby 2.7+) Uses grep for pattern matching against method results

Parameters:

  • method (Symbol, String)

    The method to call on each element

  • pattern (Regexp, Object)

    Pattern to match against

Returns:

  • (Array)

    Array of matching elements



376
377
378
379
380
381
382
383
384
# File 'lib/ww.rb', line 376

def match_by(method, pattern)
  raise ArgumentError,
        'Method must be a Symbol or String' unless method.is_a?(Symbol) || method.is_a?(String)

  grep { |item| pattern === item.send(method) }
rescue NoMethodError => err
  warn "Method #{method} not available on some items: #{err.message}"
  []
end

#partition_by(method, value) ⇒ Array

Partition elements based on method result

Parameters:

  • method (Symbol, String)

    The method to call on each element

  • value (Object)

    The value to partition by

Returns:

  • (Array)

    Array containing [matching_elements, non_matching_elements]



391
392
393
394
395
396
397
398
399
# File 'lib/ww.rb', line 391

def partition_by(method, value)
  raise ArgumentError,
        'Method must be a Symbol or String' unless method.is_a?(Symbol) || method.is_a?(String)

  partition { |item| item.respond_to?(method) && item.send(method) == value }
rescue NoMethodError => err
  warn "Method #{method} not available on some items: #{err.message}"
  [[], self]
end

#pluck(key) ⇒ Object



6
7
8
# File 'lib/array.rb', line 6

def pluck(key)
  map { |hash| hash[key] if hash.is_a?(Hash) }.compact
end

#process_and_conditionally_delete!Array

Processes each element of the array, yielding the previous, current, and next elements to the given block. Deletes the current element if the block returns true.

Returns:

  • (Array)

    The modified array after conditional deletions.



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/array.rb', line 13

def process_and_conditionally_delete!
  i = 0
  while i < length
    prev_item = self[i - 1] unless i.zero?
    current_item = self[i]
    next_item = self[i + 1]

    should_delete = yield prev_item, current_item, next_item
    if should_delete
      delete_at(i)
    else
      i += 1
    end
  end

  self
end

#reject_by(method = nil, value = nil, &block) ⇒ Array

Reject elements where the specified method matches a value Supports both method-based and block-based filtering

Parameters:

  • method (Symbol, String, nil) (defaults to: nil)

    The method to call on each element

  • value (Object, nil) (defaults to: nil)

    The value to match against

  • block (Proc, nil)

    Optional block for custom filtering logic

Returns:

  • (Array)

    Array of non-matching elements



408
409
410
411
412
413
414
415
416
417
418
419
420
# File 'lib/ww.rb', line 408

def reject_by(method = nil, value = nil, &block)
  if block_given?
    reject(&block)
  else
    raise ArgumentError,
          'Method must be a Symbol or String' unless method.is_a?(Symbol) || method.is_a?(String)

    reject { |item| item.respond_to?(method) && item.send(method) == value }
  end
rescue NoMethodError => err
  warn "Method #{method} not available on some items: #{err.message}"
  self
end

#select_by(method = nil, value = nil, &block) ⇒ Array

Select elements where the specified method matches a value Supports both method-based and block-based filtering

Parameters:

  • method (Symbol, String, nil) (defaults to: nil)

    The method to call on each element

  • value (Object, nil) (defaults to: nil)

    The value to match against

  • block (Proc, nil)

    Optional block for custom filtering logic

Returns:

  • (Array)

    Array of matching elements



429
430
431
432
433
434
435
436
437
438
439
440
441
# File 'lib/ww.rb', line 429

def select_by(method = nil, value = nil, &block)
  if block_given?
    select(&block)
  else
    raise ArgumentError,
          'Method must be a Symbol or String' unless method.is_a?(Symbol) || method.is_a?(String)

    select { |item| item.respond_to?(method) && item.send(method) == value }
  end
rescue NoMethodError => err
  warn "Method #{method} not available on some items: #{err.message}"
  []
end

#select_where(conditions = {}) ⇒ Array

Select elements using hash-based conditions All conditions must match for an element to be included

Parameters:

  • conditions (Hash) (defaults to: {})

    Hash of method => value pairs

Returns:

  • (Array)

    Array of matching elements



448
449
450
451
452
453
454
455
456
457
# File 'lib/ww.rb', line 448

def select_where(conditions = {})
  select do |item|
    conditions.all? do |method, value|
      item.respond_to?(method) && item.send(method) == value
    end
  end
rescue NoMethodError => err
  warn "Some methods not available on items: #{err.message}"
  []
end