Project

General

Profile

Feature #14052 ยป securerandom-with_chars.diff

kyanagi (Kouhei Yanagita), 10/25/2017 05:23 AM

View differences:

lib/securerandom.rb
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
......
#
# 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
......
# +NotImplementedError+ is raised.
def alphanumeric(n=nil)
n = 16 if n.nil?
choose(ALPHANUMERIC, n)
with_chars(ALPHANUMERIC, n)
end
end
test/test_securerandom.rb
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
    (1-1/1)