class Faker::String

Public Class Methods

random(legacy_length = NOT_GIVEN, length: 32) click to toggle source
# File lib/faker/default/string.rb, line 6
def random(legacy_length = NOT_GIVEN, length: 32)
  warn_for_deprecated_arguments do |keywords|
    keywords << :length if legacy_length != NOT_GIVEN
  end

  utf8string select_a length
end

Private Class Methods

char_space_ratio() click to toggle source
# File lib/faker/default/string.rb, line 35
def char_space_ratio
  sample [2, [3] * 5, [5] * 17, [7] * 11, 11, 191].flatten
end
select_a(length) click to toggle source
# File lib/faker/default/string.rb, line 16
def select_a(length)
  if length.class.included_modules.include? Enumerable
    select_a sample(length.to_enum.to_a)
  else
    length # recursive base case
  end
end
space_or_utf8_char(ratio) click to toggle source
# File lib/faker/default/string.rb, line 31
def space_or_utf8_char(ratio)
  sample [32.chr(Encoding::UTF_8), [utf8character] * ratio].flatten
end
utf8character() click to toggle source
# File lib/faker/default/string.rb, line 39
def utf8character
  sample([
           rand(32..90), # latin alphabet
           rand(128),             # 7-bit ASCII
           rand(0xd800),          # utf-8 codepoints below utf-16 surrogate halves
           rand(57_344..1_114_111) # utf-8 codepoints above utf-16 surrogate halves
         ]).chr(Encoding::UTF_8)
end
utf8string(length) click to toggle source
# File lib/faker/default/string.rb, line 24
def utf8string(length)
  ratio = char_space_ratio
  (1..length.to_i).map do
    space_or_utf8_char ratio
  end.join
end