Module: Bases::Helpers

Defined in:
lib/bases/helpers.rb

Overview

Some miscellaneous helper functions.

Class Method Summary collapse

Class Method Details

.are_there_duplicates?(ary) ⇒ bool

Return true if there are duplicate elements in ary.



8
9
10
# File 'lib/bases/helpers.rb', line 8

def self.are_there_duplicates?(ary)
  ary.uniq.size != ary.size
end

.base_to_array(base) ⇒ Array<String>

Return an array version of base. An array version of base means an array of strings. If base is already an array, a copy of that array but with all elements converted to strings is returned. If base is an integer (in base 10 :D) a range-like array going from 0 to base is returned.

Raises:

  • (DuplicateDigitsError)

    if there are duplicate digits in the base (if it was passed as an array).



21
22
23
24
25
26
27
28
29
30
# File 'lib/bases/helpers.rb', line 21

def self.base_to_array(base)
  base = (base.is_a?(Array) ? base : (0...base)).map(&:to_s)

  if are_there_duplicates?(base)
    fail Bases::DuplicateDigitsError,
      'There are duplicate digits in the base'
  else
    base
  end
end

.only_valid_digits?(digits, base) ⇒ boolean

Check whether digits contains some digits that are not in base.



36
37
38
# File 'lib/bases/helpers.rb', line 36

def self.only_valid_digits?(digits, base)
  digits.all? { |digit| base.include?(digit) }
end