Module: Ronin::Support::Text::Random

Defined in:
lib/ronin/support/text/random.rb,
lib/ronin/support/text/random/mixin.rb

Overview

Methods for generating random text.

Since:

  • 1.0.0

Defined Under Namespace

Modules: Mixin

Class Method Summary collapse

Class Method Details

.alpha(n = 1) ⇒ String

A random alphabetic string.

Parameters:

  • n (Integer, Range<Integer>) (defaults to: 1)

    The desired length of the String.

Returns:

  • (String)

    A random alphabetic string.

See Also:

Since:

  • 1.0.0



264
265
266
# File 'lib/ronin/support/text/random.rb', line 264

def self.alpha(n=1)
  Chars::ALPHA.random_string(n)
end

.alpha_numeric(n = 1) ⇒ String

A random alpha-numeric string.

Parameters:

  • n (Integer, Range<Integer>) (defaults to: 1)

    The desired length of the String.

Returns:

  • (String)

    A random alpha-numeric string.

See Also:

Since:

  • 1.0.0



279
280
281
# 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) ⇒ String

A random ASCII string.

Parameters:

  • n (Integer, Range<Integer>) (defaults to: 1)

    The desired length of the String.

Returns:

  • (String)

    A random ASCII string.

See Also:

Since:

  • 1.0.0



413
414
415
# File 'lib/ronin/support/text/random.rb', line 413

def self.ascii(n=1)
  Chars::ASCII.random_string(n)
end

.control(n = 1) ⇒ String

A random control-character string.

Parameters:

  • n (Integer, Range<Integer>) (defaults to: 1)

    The desired length of the String.

Returns:

  • (String)

    A random control-character string.

See Also:

Since:

  • 1.0.0



383
384
385
# File 'lib/ronin/support/text/random.rb', line 383

def self.control(n=1)
  Chars::CONTROL.random_string(n)
end

.digits(n = 1) ⇒ String

Alias for numeric.

Parameters:

  • n (Integer, Range<Integer>) (defaults to: 1)

    The desired length of the String.

Returns:

  • (String)

    A random decimal-digit string.

See Also:

  • random_numeric

Since:

  • 1.0.0



99
100
101
# File 'lib/ronin/support/text/random.rb', line 99

def self.digits(n=1)
  numeric(n)
end

.hex(n = 1) ⇒ String

A random hexadecimal string.

Parameters:

  • n (Integer, Range<Integer>) (defaults to: 1)

    The desired length of the String.

Returns:

  • (String)

    A random hexadecimal string.

See Also:

Since:

  • 1.0.0



189
190
191
# File 'lib/ronin/support/text/random.rb', line 189

def self.hex(n=1)
  Chars::HEXADECIMAL.random_string(n)
end

.lower_alpha(n = 1) ⇒ String

Alias for lowercase_alpha.

Parameters:

  • n (Integer, Range<Integer>) (defaults to: 1)

    The desired length of the String.

Returns:

  • (String)

    The lower-case alphabetic character set.

See Also:

Since:

  • 1.0.0



249
250
251
# File 'lib/ronin/support/text/random.rb', line 249

def self.lower_alpha(n=1)
  lowercase_alpha(n)
end

.lower_hex(n = 1) ⇒ String

Alias for lowercase_hex.

Parameters:

  • n (Integer, Range<Integer>) (defaults to: 1)

    The desired length of the String.

Returns:

  • (String)

    The lower-case hexadecimal character set.

See Also:

Since:

  • 1.0.0



174
175
176
# File 'lib/ronin/support/text/random.rb', line 174

def self.lower_hex(n=1)
  lowercase_hex(n)
end

.lowercase_alpha(n = 1) ⇒ String

The lower-case alphabetic character set.

Parameters:

  • n (Integer, Range<Integer>) (defaults to: 1)

    The desired length of the String.

Returns:

  • (String)

    The lower-case alphabetic character set.

See Also:

Since:

  • 1.0.0



234
235
236
# 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) ⇒ String

The lower-case hexadecimal character set.

Parameters:

  • n (Integer, Range<Integer>) (defaults to: 1)

    The desired length of the String.

Returns:

  • (String)

    The lower-case hexadecimal character set.

See Also:

Since:

  • 1.0.0



159
160
161
# 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) ⇒ String

Generates a random String of numeric characters.

Parameters:

  • n (Integer, Range<Integer>) (defaults to: 1)

    The desired length of the String.

Returns:

  • (String)

    A random decimal-digit string.

See Also:

Since:

  • 1.0.0



84
85
86
# File 'lib/ronin/support/text/random.rb', line 84

def self.numeric(n=1)
  Chars::NUMERIC.random_string(n)
end

.octal(n = 1) ⇒ String

A random octal-digit string.

Parameters:

  • n (Integer, Range<Integer>) (defaults to: 1)

    The desired length of the String.

Returns:

  • (String)

    A random octal-digit string.

See Also:

Since:

  • 1.0.0



114
115
116
# File 'lib/ronin/support/text/random.rb', line 114

def self.octal(n=1)
  Chars::OCTAL.random_string(n)
end

.printable(n = 1) ⇒ String

The set of printable characters, including spaces.

Parameters:

  • n (Integer, Range<Integer>) (defaults to: 1)

    The desired length of the String.

Returns:

  • (String)

    A random printable string.

See Also:

Since:

  • 1.0.0



368
369
370
# File 'lib/ronin/support/text/random.rb', line 368

def self.printable(n=1)
  Chars::PRINTABLE.random_string(n)
end

.punctuation(n = 1) ⇒ String

A random punctuation string.

Parameters:

  • n (Integer, Range<Integer>) (defaults to: 1)

    The desired length of the String.

Returns:

  • (String)

    A random punctuation string.

See Also:

Since:

  • 1.0.0



294
295
296
# File 'lib/ronin/support/text/random.rb', line 294

def self.punctuation(n=1)
  Chars::PUNCTUATION.random_string(n)
end

.signed_ascii(n = 1) ⇒ String

The signed ASCII character set.

Parameters:

  • n (Integer, Range<Integer>) (defaults to: 1)

    The desired length of the String.

Returns:

  • (String)

    The signed ASCII character set.

See Also:

Since:

  • 1.0.0



398
399
400
# 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) ⇒ String

A random whitespace string.

Parameters:

  • n (Integer, Range<Integer>) (defaults to: 1)

    The desired length of the String.

Returns:

See Also:

Since:

  • 1.0.0



338
339
340
# File 'lib/ronin/support/text/random.rb', line 338

def self.space(n=1)
  whitespace(n)
end

.swapcase(string) ⇒ String

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

Examples:

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."

Parameters:

  • string (String)

    The string to randomize the case of.

Returns:

  • (String)

    The new String with randomized case.

Since:

  • 1.0.0



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# 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) ⇒ String

A random symbolic string.

Parameters:

  • n (Integer, Range<Integer>) (defaults to: 1)

    The desired length of the String.

Returns:

  • (String)

    A random symbolic string.

See Also:

Since:

  • 1.0.0



309
310
311
# File 'lib/ronin/support/text/random.rb', line 309

def self.symbols(n=1)
  Chars::SYMBOLS.random_string(n)
end

.upper_alpha(n = 1) ⇒ String

Alias for uppercase_alpha.

Parameters:

  • n (Integer, Range<Integer>) (defaults to: 1)

    The desired length of the String.

Returns:

  • (String)

    The upper-case alphabetic character set.

See Also:

Since:

  • 1.0.0



219
220
221
# File 'lib/ronin/support/text/random.rb', line 219

def self.upper_alpha(n=1)
  uppercase_alpha(n)
end

.upper_hex(n = 1) ⇒ String

Alias for uppercase_hex.

Parameters:

  • n (Integer, Range<Integer>) (defaults to: 1)

    The desired length of the String.

Returns:

  • (String)

    The upper-case hexadecimal character set.

See Also:

Since:

  • 1.0.0



144
145
146
# File 'lib/ronin/support/text/random.rb', line 144

def self.upper_hex(n=1)
  uppercase_hex(n)
end

.uppercase_alpha(n = 1) ⇒ String

The upper-case alphabetic character set.

Parameters:

  • n (Integer, Range<Integer>) (defaults to: 1)

    The desired length of the String.

Returns:

  • (String)

    The upper-case alphabetic character set.

See Also:

Since:

  • 1.0.0



204
205
206
# 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) ⇒ String

The upper-case hexadecimal character set.

Parameters:

  • n (Integer, Range<Integer>) (defaults to: 1)

    The desired length of the String.

Returns:

  • (String)

    The upper-case hexadecimal character set.

See Also:

Since:

  • 1.0.0



129
130
131
# 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) ⇒ String

The set of printable characters, not including spaces.

Parameters:

  • n (Integer, Range<Integer>) (defaults to: 1)

    The desired length of the String.

Returns:

  • (String)

    A random visible string.

See Also:

Since:

  • 1.0.0



353
354
355
# File 'lib/ronin/support/text/random.rb', line 353

def self.visible(n=1)
  Chars::VISIBLE.random_string(n)
end

.whitespace(n = 1) ⇒ String

A random whitespace string.

Parameters:

  • n (Integer, Range<Integer>) (defaults to: 1)

    The desired length of the String.

Returns:

  • (String)

    A random whitespace string.

See Also:

Since:

  • 1.0.0



324
325
326
# File 'lib/ronin/support/text/random.rb', line 324

def self.whitespace(n=1)
  Chars::WHITESPACE.random_string(n)
end