module Ronin::Support::Text::Random

Methods for generating random text.

@api public

@since 1.0.0

Public Class Methods

alpha(n=1) click to toggle source

A random alphabetic string.

@param [Integer, Range<Integer>] n

The desired length of the String.

@return [String]

A random alphabetic string.

@see rubydoc.info/gems/chars/Chars#ALPHA-constant

# File lib/ronin/support/text/random.rb, line 264
def self.alpha(n=1)
  Chars::ALPHA.random_string(n)
end
alpha_numeric(n=1) click to toggle source

A random alpha-numeric string.

@param [Integer, Range<Integer>] n

The desired length of the String.

@return [String]

A random alpha-numeric string.

@see rubydoc.info/gems/chars/Chars#ALPHA_NUMERIC-constant

# File lib/ronin/support/text/random.rb, line 279
def self.alpha_numeric(n=1)
  Chars::ALPHA_NUMERIC.random_string(n)
end
ascii(n=1) click to toggle source

A random ASCII string.

@param [Integer, Range<Integer>] n

The desired length of the String.

@return [String]

A random ASCII string.

@see rubydoc.info/gems/chars/Chars#ASCII-constant

# File lib/ronin/support/text/random.rb, line 413
def self.ascii(n=1)
  Chars::ASCII.random_string(n)
end
control(n=1) click to toggle source

A random control-character string.

@param [Integer, Range<Integer>] n

The desired length of the String.

@return [String]

A random control-character string.

@see rubydoc.info/gems/chars/Chars#CONTROL-constant

# File lib/ronin/support/text/random.rb, line 383
def self.control(n=1)
  Chars::CONTROL.random_string(n)
end
digits(n=1) click to toggle source

Alias for {numeric}.

@param [Integer, Range<Integer>] n

The desired length of the String.

@return [String]

A random decimal-digit string.

@see random_numeric

# File lib/ronin/support/text/random.rb, line 99
def self.digits(n=1)
  numeric(n)
end
hex(n=1) click to toggle source

A random hexadecimal string.

@param [Integer, Range<Integer>] n

The desired length of the String.

@return [String]

A random hexadecimal string.

@see rubydoc.info/gems/chars/Chars#HEXADECIMAL-constant

# File lib/ronin/support/text/random.rb, line 189
def self.hex(n=1)
  Chars::HEXADECIMAL.random_string(n)
end
lower_alpha(n=1) click to toggle source

Alias for {lowercase_alpha}.

@param [Integer, Range<Integer>] n

The desired length of the String.

@return [String]

The lower-case alphabetic character set.

@see lowercase_alpha

# File lib/ronin/support/text/random.rb, line 249
def self.lower_alpha(n=1)
  lowercase_alpha(n)
end
lower_hex(n=1) click to toggle source

Alias for {lowercase_hex}.

@param [Integer, Range<Integer>] n

The desired length of the String.

@return [String]

The lower-case hexadecimal character set.

@see lowercase_hex

# File lib/ronin/support/text/random.rb, line 174
def self.lower_hex(n=1)
  lowercase_hex(n)
end
lowercase_alpha(n=1) click to toggle source

The lower-case alphabetic character set.

@param [Integer, Range<Integer>] n

The desired length of the String.

@return [String]

The lower-case alphabetic character set.

@see rubydoc.info/gems/chars/Chars#LOWERCASE_ALPHA-constant

# File lib/ronin/support/text/random.rb, line 234
def self.lowercase_alpha(n=1)
  Chars::LOWERCASE_ALPHA.random_string(n)
end
lowercase_hex(n=1) click to toggle source

The lower-case hexadecimal character set.

@param [Integer, Range<Integer>] n

The desired length of the String.

@return [String]

The lower-case hexadecimal character set.

@see rubydoc.info/gems/chars/Chars#LOWERCASE_HEXADECIMAL-constant

# File lib/ronin/support/text/random.rb, line 159
def self.lowercase_hex(n=1)
  Chars::LOWERCASE_HEXADECIMAL.random_string(n)
end
numeric(n=1) click to toggle source

Generates a random String of numeric characters.

@param [Integer, Range<Integer>] n

The desired length of the String.

@return [String]

A random decimal-digit string.

@see rubydoc.info/gems/chars/Chars#NUMERIC-constant

# File lib/ronin/support/text/random.rb, line 84
def self.numeric(n=1)
  Chars::NUMERIC.random_string(n)
end
octal(n=1) click to toggle source

A random octal-digit string.

@param [Integer, Range<Integer>] n

The desired length of the String.

@return [String]

A random octal-digit string.

@see rubydoc.info/gems/chars/Chars#OCTAL-constant

# File lib/ronin/support/text/random.rb, line 114
def self.octal(n=1)
  Chars::OCTAL.random_string(n)
end
printable(n=1) click to toggle source

The set of printable characters, including spaces.

@param [Integer, Range<Integer>] n

The desired length of the String.

@return [String]

A random printable string.

@see rubydoc.info/gems/chars/Chars#PRINTABLE-constant

# File lib/ronin/support/text/random.rb, line 368
def self.printable(n=1)
  Chars::PRINTABLE.random_string(n)
end
punctuation(n=1) click to toggle source

A random punctuation string.

@param [Integer, Range<Integer>] n

The desired length of the String.

@return [String]

A random punctuation string.

@see rubydoc.info/gems/chars/Chars#PUNCTUATION-constant

# File lib/ronin/support/text/random.rb, line 294
def self.punctuation(n=1)
  Chars::PUNCTUATION.random_string(n)
end
signed_ascii(n=1) click to toggle source

The signed ASCII character set.

@param [Integer, Range<Integer>] n

The desired length of the String.

@return [String]

The signed ASCII character set.

@see rubydoc.info/gems/chars/Chars#SIGNED_ASCII-constant

# File lib/ronin/support/text/random.rb, line 398
def self.signed_ascii(n=1)
  Chars::SIGNED_ASCII.random_string(n)
end
space(n=1) click to toggle source

A random whitespace string.

@param [Integer, Range<Integer>] n

The desired length of the String.

@return [String]

@see whitespace

# File lib/ronin/support/text/random.rb, line 338
def self.space(n=1)
  whitespace(n)
end
swapcase(string) click to toggle source

Creates a new String by randomizing the case of each character in the string.

@param [String] string

The string to randomize the case of.

@return [String]

The new String with randomized case.

@example

Text::Random.swapcase("a")
# => "A"
Text::Random.swapcase("ab")
# => "aB"
Text::Random.swapcase("foo")
# => "FoO"
Text::Random.swapcase("The quick brown fox jumps over 13 lazy dogs.")
# => "the quIcK broWn fox Jumps oveR 13 lazY Dogs."
# File lib/ronin/support/text/random.rb, line 52
def self.swapcase(string)
  candidates = []

  string.each_char.with_index do |char,index|
    if char =~ /\p{L}/
      candidates << index
    end
  end

  new_string = string.dup

  # ensure that at least one character is swap-cased, but not all
  num_swaps = rand(1..(candidates.length - 1)) || 1

  candidates.sample(num_swaps).each do |index|
    new_string[index] = new_string[index].swapcase
  end

  return new_string
end
symbols(n=1) click to toggle source

A random symbolic string.

@param [Integer, Range<Integer>] n

The desired length of the String.

@return [String]

A random symbolic string.

@see rubydoc.info/gems/chars/Chars#SYMBOLS-constant

# File lib/ronin/support/text/random.rb, line 309
def self.symbols(n=1)
  Chars::SYMBOLS.random_string(n)
end
upper_alpha(n=1) click to toggle source

Alias for {uppercase_alpha}.

@param [Integer, Range<Integer>] n

The desired length of the String.

@return [String]

The upper-case alphabetic character set.

@see uppercase_alpha

# File lib/ronin/support/text/random.rb, line 219
def self.upper_alpha(n=1)
  uppercase_alpha(n)
end
upper_hex(n=1) click to toggle source

Alias for {uppercase_hex}.

@param [Integer, Range<Integer>] n

The desired length of the String.

@return [String]

The upper-case hexadecimal character set.

@see uppercase_hex

# File lib/ronin/support/text/random.rb, line 144
def self.upper_hex(n=1)
  uppercase_hex(n)
end
uppercase_alpha(n=1) click to toggle source

The upper-case alphabetic character set.

@param [Integer, Range<Integer>] n

The desired length of the String.

@return [String]

The upper-case alphabetic character set.

@see rubydoc.info/gems/chars/Chars#UPPERCASE_ALPHA-constant

# File lib/ronin/support/text/random.rb, line 204
def self.uppercase_alpha(n=1)
  Chars::UPPERCASE_ALPHA.random_string(n)
end
uppercase_hex(n=1) click to toggle source

The upper-case hexadecimal character set.

@param [Integer, Range<Integer>] n

The desired length of the String.

@return [String]

The upper-case hexadecimal character set.

@see rubydoc.info/gems/chars/Chars#UPPERCASE_HEXADECIMAL#constant

# File lib/ronin/support/text/random.rb, line 129
def self.uppercase_hex(n=1)
  Chars::UPPERCASE_HEXADECIMAL.random_string(n)
end
visible(n=1) click to toggle source

The set of printable characters, not including spaces.

@param [Integer, Range<Integer>] n

The desired length of the String.

@return [String]

A random visible string.

@see rubydoc.info/gems/chars/Chars#VISIBLE-constant

# File lib/ronin/support/text/random.rb, line 353
def self.visible(n=1)
  Chars::VISIBLE.random_string(n)
end
whitespace(n=1) click to toggle source

A random whitespace string.

@param [Integer, Range<Integer>] n

The desired length of the String.

@return [String]

A random whitespace string.

@see rubydoc.info/gems/chars/Chars#WHITESPACE-constant

# File lib/ronin/support/text/random.rb, line 324
def self.whitespace(n=1)
  Chars::WHITESPACE.random_string(n)
end