diff --git a/lib/securerandom.rb b/lib/securerandom.rb index 97911d4d6a..3223c2d461 100644 --- a/lib/securerandom.rb +++ b/lib/securerandom.rb @@ -226,7 +226,7 @@ def uuid self.bytes(n) end - # SecureRandom.choose generates a string that randomly draws from a + # SecureRandom.with_chars generates a string that randomly draws from a # source array of characters. # # The argument _source_ specifies the array of characters from which @@ -236,12 +236,12 @@ def uuid # # The result may contain whatever characters are in the source array. # - # p SecureRandom.choose([*'l'..'r']) #=> "lmrqpoonmmlqlron" - # p SecureRandom.choose([*'0'..'9'], 5) #=> "27309" + # p SecureRandom.with_chars([*'l'..'r']) #=> "lmrqpoonmmlqlron" + # p SecureRandom.with_chars([*'0'..'9'], 5) #=> "27309" # # If a secure random number generator is not available, # +NotImplementedError+ is raised. - private def choose(source, n) + def with_chars(source, n) size = source.size m = 1 limit = size @@ -288,7 +288,7 @@ def uuid # +NotImplementedError+ is raised. def alphanumeric(n=nil) n = 16 if n.nil? - choose(ALPHANUMERIC, n) + with_chars(ALPHANUMERIC, n) end end diff --git a/test/test_securerandom.rb b/test/test_securerandom.rb index 69d24c0417..9be4ed01dc 100644 --- a/test/test_securerandom.rb +++ b/test/test_securerandom.rb @@ -151,6 +151,15 @@ def test_alphanumeric end end + def test_with_chars + source = 'abc123'.chars + 65.times do |n| + an = @it.with_chars(source, n) + assert_match(/\A[abc123]*\z/, an) + assert_equal(n, an.length) + end + end + def protect begin yield