diff --git a/lib/random/formatter.rb b/lib/random/formatter.rb index 037f9d8..e5e3de9 100644 --- a/lib/random/formatter.rb +++ b/lib/random/formatter.rb @@ -42,6 +42,12 @@ # prng.alphanumeric(10) #=> "aOxAg8BAJe" # Random.alphanumeric #=> "TmP9OsJHJLtaZYhP" # +# Generate strings from the visible printable ASSCII characters - characters 33–126 +# +# prng.ascii_printable_visible(20) #=> "BSJsDtHia\\8/P\"Ag#fy^" +# prng.ascii_printable_visible(20) #=> "L^4iJK#W%R(5PeFa;4ss" +# prng.ascii_printable_visible(20) #=> "!#=j)6u_7cO^DP "2d931510-d99f-494a-8c67-87feb05e1594" @@ -370,4 +376,27 @@ def alphanumeric(n = nil, chars: ALPHANUMERIC) n = 16 if n.nil? choose(chars, n) end + + + ASCII_PRINTABLE_VISIBLE = ('!'..'~').to_a + + # Generate a random string of characters from the printable visible ASCII space, + # characters 33–126, which is more or less the set of unaccented characters typable + # on a typical English keyboard. + # + # The argument _n_ specifies the length, in characters, of the + # string to be generated. + # + # If _n_ is not specified or is nil, 16 is assumed. + # It may be larger in the future. + # + # require 'random/formatter' + # + # Random.ascii_printable_visible #=> "A3\\HDEY?+YpZ806_" + # # or + # prng = Random.new + # prng.ascii_printable_visible(10) #=> "n5v&~Mi8{'" + def ascii_printable_visible(n = nil) + alphanumeric(n, chars: ASCII_PRINTABLE_VISIBLE) + end end diff --git a/test/ruby/test_random_formatter.rb b/test/ruby/test_random_formatter.rb index f927522..256f244 100644 --- a/test/ruby/test_random_formatter.rb +++ b/test/ruby/test_random_formatter.rb @@ -124,6 +124,14 @@ def test_alphanumeric end end + def test_ascii_printable_visible + 65.times do |n| + an = @it.ascii_printable_visible(n) + assert_match(/\A[!-~]*\z/, an) + assert_equal(n, an.length) + end + end + def test_alphanumeric_chars [ [[*"0".."9"], /\A\d*\z/],