Class: Faker::Alphanumeric
- Defined in:
- lib/faker/default/alphanumeric.rb
Constant Summary collapse
Constants inherited from Base
Base::LLetters, Base::Letters, Base::NOT_GIVEN, Base::Numbers, Base::ULetters
Class Method Summary collapse
-
.alpha(number: 32) ⇒ String
Produces a random string of alphabetic characters (no digits).
-
.alphanumeric(number: 32, min_alpha: 0, min_numeric: 0) ⇒ String
Produces a random string of alphanumeric characters.
Methods inherited from Base
bothify, disable_enforce_available_locales, fetch, fetch_all, flexible, generate, letterify, method_missing, numerify, parse, rand, rand_in_range, regexify, resolve, respond_to_missing?, sample, shuffle, shuffle!, translate, unique, with_locale
Class Method Details
.alpha(number: 32) ⇒ String
Produces a random string of alphabetic characters (no digits)
22 23 24 25 26 27 |
# File 'lib/faker/default/alphanumeric.rb', line 22 def alpha(number: 32) char_count = resolve(number) return '' if char_count.to_i < 1 Array.new(char_count) { sample(self::LLetters) }.join end |
.alphanumeric(number: 32, min_alpha: 0, min_numeric: 0) ⇒ String
Produces a random string of alphanumeric characters
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 |
# File 'lib/faker/default/alphanumeric.rb', line 46 def alphanumeric(number: 32, min_alpha: 0, min_numeric: 0) 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 shuffle!(combined).join end |