Class: Faker::Alphanumeric

Inherits:
Base
  • Object
show all
Defined in:
lib/faker/default/alphanumeric.rb

Constant Summary

Constants inherited from Base

Base::LLetters, Base::Letters, Base::NOT_GIVEN, Base::Numbers, Base::ULetters

Class Method Summary collapse

Methods inherited from Base

bothify, disable_enforce_available_locales, fetch, fetch_all, flexible, letterify, method_missing, numerify, parse, rand, rand_in_range, regexify, resolve, respond_to_missing?, sample, shuffle, translate, unique, with_locale

Class Method Details

.alpha(legacy_number = NOT_GIVEN, number: 32) ⇒ String

Produces a random string of alphabetic characters (no digits)

Examples:

Faker::Alphanumeric.alpha(number: 10) #=> "zlvubkrwga"

Parameters:

  • number (Integer) (defaults to: 32)

    The length of the string to generate

Returns:

  • (String)

Available since:

  • 1.9.2


22
23
24
25
26
27
28
29
30
# File 'lib/faker/default/alphanumeric.rb', line 22

def alpha(legacy_number = NOT_GIVEN, number: 32)
  warn_for_deprecated_arguments do |keywords|
    keywords << :number if legacy_number != NOT_GIVEN
  end
  char_count = resolve(number)
  return '' if char_count.to_i < 1

  Array.new(char_count) { sample(self::LLetters) }.join
end

.alphanumeric(legacy_number = NOT_GIVEN, number: 32, min_alpha: 0, min_numeric: 0) ⇒ String

Produces a random string of alphanumeric characters

Examples:

Faker::Alphanumeric.alphanumeric(number: 10) #=> "3yfq2phxtb"
Faker::Alphanumeric.alphanumeric(number: 10, min_alpha: 3) #=> "3yfq2phxtb"
Faker::Alphanumeric.alphanumeric(number: 10, min_alpha: 3, min_numeric: 3) #=> "3yfq2phx8b"

Parameters:

  • number (Integer) (defaults to: 32)

    The number of characters to generate

  • min_alpha (Integer) (defaults to: 0)

    The minimum number of alphabetic to add to the string

  • min_numeric (Integer) (defaults to: 0)

    The minimum number of numbers to add to the string

Returns:

  • (String)

Raises:

  • (ArgumentError)

Available since:

  • 2.1.3


49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/faker/default/alphanumeric.rb', line 49

def alphanumeric(legacy_number = NOT_GIVEN, number: 32, min_alpha: 0, min_numeric: 0)
  warn_for_deprecated_arguments do |keywords|
    keywords << :number if legacy_number != NOT_GIVEN
  end
  char_count = resolve(number)
  return '' if char_count.to_i < 1
  raise ArgumentError, 'min_alpha must be greater than or equal to 0' if min_alpha&.negative?
  raise ArgumentError, 'min_numeric must be greater than or equal to 0' if min_numeric&.negative?

  return Array.new(char_count) { sample(ALPHANUMS) }.join if min_alpha.zero? && min_numeric.zero?

  raise ArgumentError, 'min_alpha + min_numeric must be <= number' if min_alpha + min_numeric > char_count

  random_count = char_count - min_alpha - min_numeric

  alphas = Array.new(min_alpha) { sample(self::LLetters) }
  numbers = Array.new(min_numeric) { sample(self::Numbers) }
  randoms = Array.new(random_count) { sample(ALPHANUMS) }

  combined = alphas + numbers + randoms
  combined.shuffle.join
end