Skip to content

Commit

Permalink
Restore SecureRandom.alphanumeric same as Random::Formatter.alphanume…
Browse files Browse the repository at this point in the history
…ric of Ruby 3.3/3.4

Fixes #35
  • Loading branch information
hsbt committed Dec 16, 2024
1 parent 53f97f3 commit 31059a8
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 0 deletions.
10 changes: 10 additions & 0 deletions lib/securerandom.rb
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,16 @@ def bytes(n)
return gen_random(n)
end

# Compatiblity methods for Ruby 3.2, we can remove this after dropping to support Ruby 3.2
def alphanumeric(n = nil, chars: ALPHANUMERIC)
if RUBY_VERSION < '3.3'
n = 16 if n.nil?
choose(chars, n)
else
super n, chars: chars
end
end

private

# :stopdoc:
Expand Down
6 changes: 6 additions & 0 deletions test/test_securerandom.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,12 @@ def setup
@it = SecureRandom
end

def test_alphanumeric_with_chars
assert_nothing_raised(ArgumentError) do
@it.alphanumeric(1, chars: ("0".."9").to_a)
end
end

# This test took 2 minutes on my machine.
# And 65536 times loop could not be enough for forcing PID recycle.
# TODO: We should run this test only on GitHub Actions.
Expand Down

0 comments on commit 31059a8

Please sign in to comment.