diff --git a/lib/random/formatter.rb b/lib/random/formatter.rb index 1e17b24..2ed73f6 100644 --- a/lib/random/formatter.rb +++ b/lib/random/formatter.rb @@ -243,6 +243,13 @@ def alphanumeric(n=nil) # # prng.phrase #=> "kDc9y^Xyii4.rm3WB~MAgl0^pSOBn^jsQKc^WakTU!OjfSs" # prng.phrase(20) #=> "rKz4p-QihCf.zHff4^ukRGn" + # + # If _separators_ is +nil+ or empty, the result will consist of only + # one chunk without separators. + # + # require 'random/formatter' + # + # prng.phrase(10, separators: nil) #=> "YAzMKLQT8G" def phrase(n = 40, chunk: CHUNK_SIZE, separators: PUNCT, exclude: EXCLUDE) raise ArgumentError, "invalid chunk size" unless chunk > 0 case n @@ -257,15 +264,22 @@ def phrase(n = 40, chunk: CHUNK_SIZE, separators: PUNCT, exclude: EXCLUDE) when String separators = separators.chars end - if separators.size > 1 - sep = proc {choose(separators, 1)} - else - sep = proc {separators[0]} + if separators + case + when separators.size > 1 + sep = proc {choose(separators, 1)} + when separators.size > 0 + sep = proc {separators[0]} + end end - w, d = n.divmod(chunk + 1) - if d.zero? and w > 1 - w -= 1 - d = chunk + 1 + if sep + w, d = n.divmod(chunk + 1) + if d.zero? and w > 1 + w -= 1 + d = chunk + 1 + end + else + w, d = 0, n end source = ALPHANUMERIC source -= exclude if exclude diff --git a/test/ruby/test_random_formatter.rb b/test/ruby/test_random_formatter.rb index 689ae4d..a922f52 100644 --- a/test/ruby/test_random_formatter.rb +++ b/test/ruby/test_random_formatter.rb @@ -96,6 +96,9 @@ def test_phrase s.scan(/\w+/) do |c| assert_operator(c.size, :<=, formatter::CHUNK_SIZE) end + + s = @it.phrase(10, separators: nil) + assert_match(/\A\w{10}\z/, s) end def assert_in_range(range, result, mesg = nil)