Skip to content

Commit fbb36e3

Browse files
committed
Restore SecureRandom.alphanumeric same as Random::Formatter.alphanumeric of Ruby 3.3/3.4
Fixes #35
1 parent 53f97f3 commit fbb36e3

File tree

2 files changed

+16
-0
lines changed

2 files changed

+16
-0
lines changed

lib/securerandom.rb

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,16 @@ def bytes(n)
5151
return gen_random(n)
5252
end
5353

54+
# Compatibility methods for Ruby 3.2, we can remove this after dropping to support Ruby 3.2
55+
def alphanumeric(n = nil, chars: ALPHANUMERIC)
56+
if RUBY_VERSION < '3.3'
57+
n = 16 if n.nil?
58+
choose(chars, n)
59+
else
60+
super n, chars: chars
61+
end
62+
end
63+
5464
private
5565

5666
# :stopdoc:

test/test_securerandom.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,12 @@ def setup
99
@it = SecureRandom
1010
end
1111

12+
def test_alphanumeric_with_chars
13+
assert_nothing_raised(ArgumentError) do
14+
@it.alphanumeric(1, chars: ("0".."9").to_a)
15+
end
16+
end
17+
1218
# This test took 2 minutes on my machine.
1319
# And 65536 times loop could not be enough for forcing PID recycle.
1420
# TODO: We should run this test only on GitHub Actions.

0 commit comments

Comments
 (0)