Class: Array

Inherits:
Object show all
Defined in:
lib/ruby-rails-extensions/extensions/pipe.rb,
lib/ruby-rails-extensions/extensions/overlaps.rb,
lib/ruby-rails-extensions/extensions/uniq_map.rb,
lib/ruby-rails-extensions/extensions/find_bang.rb,
lib/ruby-rails-extensions/extensions/only_some.rb,
lib/ruby-rails-extensions/extensions/find_dupes.rb,
lib/ruby-rails-extensions/extensions/first_dupe.rb,
lib/ruby-rails-extensions/extensions/compact_map.rb,
lib/ruby-rails-extensions/extensions/weighted_sum.rb,
lib/ruby-rails-extensions/extensions/to_or_sentence.rb,
lib/ruby-rails-extensions/extensions/flatten_compact.rb

Instance Method Summary collapse

Instance Method Details

#compact_mapArray

Similar to filter_map but excludes nil instead of falsy

Returns:



9
10
11
12
13
14
15
16
17
18
# File 'lib/ruby-rails-extensions/extensions/compact_map.rb', line 9

def compact_map
  return to_enum(:compact_map) unless block_given?

  r = []
  each do |*args|
    result = yield(*args)
    r.push(result) unless result.nil?
  end
  r
end

#find!(&block) ⇒ Object

:nodoc:



8
9
10
11
12
13
# File 'lib/ruby-rails-extensions/extensions/find_bang.rb', line 8

def find!(&block)
  x = find(&block)
  raise(RubyRailsExtensions::Errors::NotFoundError, 'Not found') if x.nil?

  x
end

#find_dupes(threshold = 1) ⇒ Array

Returns excluding items that appear more than threshold times.

Parameters:

  • threshold (Integer) (defaults to: 1)

    default 1

Returns:

  • (Array)

    excluding items that appear more than threshold times



8
9
10
11
12
13
# File 'lib/ruby-rails-extensions/extensions/find_dupes.rb', line 8

def find_dupes(threshold = 1)
  results = select { |item| count(item) > threshold }
  results.uniq!

  results
end

#first_dupe(threshold = 1) ⇒ Object?

Returns first item that appears more than threshold times.

Parameters:

  • threshold (Integer) (defaults to: 1)

    default 1

Returns:

  • (Object, nil)

    first item that appears more than threshold times



6
7
8
# File 'lib/ruby-rails-extensions/extensions/first_dupe.rb', line 6

def first_dupe(threshold = 1)
  find { |item| count(item) > threshold }
end

#flatten_compact!Array Also known as: flat_pact!

Flattens and compacts an array

Returns:

See Also:

  • #flatten!
  • #compact!


10
11
12
13
14
15
16
# File 'lib/ruby-rails-extensions/extensions/flatten_compact.rb', line 10

def flatten_compact!
  flatten!

  compact!

  self
end

#only_some?(*meth_args) ⇒ Boolean

Same as ‘my_array.any?(&block) && !my_array.all?(&block)`

Returns:

  • (Boolean)


8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/ruby-rails-extensions/extensions/only_some.rb', line 8

def only_some?(*meth_args)
  # Check arg size since `any?` arg take presence over block
  return any?(*meth_args) && !all?(*meth_args) if meth_args.size.positive?

  any = false
  all = true

  each do |*args|
    result = yield(*args)

    any ||= result
    all &&= result

    break if any && !all
  end

  return any && !all
end

#overlaps?(other) ⇒ Boolean

Returns:

  • (Boolean)


8
9
10
# File 'lib/ruby-rails-extensions/extensions/overlaps.rb', line 8

def overlaps?(other)
  !(self & Array(other)).empty?
end

#pipe(*fns) ⇒ Array

Replace my_array.map(&:method1).map(&:method2) with my_array.pipe(:method1, :method2)

Returns:



9
10
11
# File 'lib/ruby-rails-extensions/extensions/pipe.rb', line 9

def pipe(*fns)
  map { |a| fns.reduce(a, &:send) }
end

#to_or_sentenceString

Returns:



8
9
10
# File 'lib/ruby-rails-extensions/extensions/to_or_sentence.rb', line 8

def to_or_sentence
  to_sentence(two_words_connector: ' or ', last_word_connector: ', or ')
end

#uniq_mapArray, Enumerable

Same as ‘my_array.map(&block).uniq`

Returns:



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/ruby-rails-extensions/extensions/uniq_map.rb', line 8

def uniq_map
  return to_enum(:uniq_map) unless block_given?

  results = []

  each do |*args|
    result = yield(*args)

    next if results.include?(result)

    results.push(result)
  end

  return results
end

#weighted_sumInteger

Sums the array with each element weighing 10 times as the next. Expects self to be [Array<Numeric>]

Returns:

  • (Integer)

Raises:



11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/ruby-rails-extensions/extensions/weighted_sum.rb', line 11

def weighted_sum
  each_with_index.sum do |val, index|
    unless Numeric === val
      raise(
        RubyRailsExtensions::Errors::NonNumericError,
        "Wrong element type (given #{val.class}, expected Numeric)"
      )
    end

    val * (10**(size - index))
  end
end