Class: String

Inherits:
Object
  • Object
show all
Defined in:
lib/dsl/monkeypatches.rb

Overview

Enchancement of String class to generate random sample based on the pattern given.

Instance Method Summary collapse

Instance Method Details

#(size: 32, symbols: [*('A'..'Z'), *('а'..'я'), *('0'..'9'), *[' ']*10]) ⇒ String Also known as: any

TODO:

Possible wanna use Faker here

Generates random sample of String.

Examples:

To create the string of length 12, consisting of lowercased latin letters:

s1 = ('a'..'z').to_chars. 12   # ⇒ fughtksnewqp
s2 = "".(12, ('a'..'z'))       # ⇒ jiuuoiqwbjty

Parameters:

  • size (Fixnum) (defaults to: 32)

    the size of the sample to generate.

  • symbols (Array<Char>) (defaults to: [*('A'..'Z'), *('а'..'я'), *('0'..'9'), *[' ']*10])

    the list of characters used to generate the sample.

Returns:

  • (String)

    the string of the given length, consisting of random characters from the given set.

Raises:

See Also:



58
59
60
61
62
63
64
65
66
67
# File 'lib/dsl/monkeypatches.rb', line 58

def (size: 32, symbols: [*('A'..'Z'), *('а'..'я'), *('0'..'9'), *[' ']*10])
  syms = case
         when !self.empty? then self.scan('.')
         when !(Array === symbols) && symbols.respond_to?(:to_a) then symbols.to_a
         else symbols
         end
  raise ArgumentError.new("`:symbols` argument class must support `#sample` method (given #{symbols})") \
    unless syms.respond_to? :sample
  "".tap { |v| size.times { v << syms.sample } }.squeeze
end