Skip to content

Commit

Permalink
Merge pull request #36 from ruby/restore-alphanumeric-method
Browse files Browse the repository at this point in the history
Restore SecureRandom.alphanumeric with 0.3.x behavior
  • Loading branch information
hsbt authored Dec 16, 2024
2 parents 53f97f3 + 2c8cdfb commit 168d94f
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 0 deletions.
6 changes: 6 additions & 0 deletions lib/securerandom.rb
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,12 @@ def bytes(n)
return gen_random(n)
end

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

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 168d94f

Please sign in to comment.