aboutsummaryrefslogtreecommitdiffstats
path: root/lib/random
diff options
context:
space:
mode:
authorNobuyoshi Nakada <nobu@ruby-lang.org>2023-08-29 10:56:56 +0900
committerNobuyoshi Nakada <nobu@ruby-lang.org>2023-08-29 10:56:56 +0900
commit7e5c662a6f2e8435f8103bc16185bed6759cc557 (patch)
treec8e6fb7cbc431e59400cf6f9200a335462b4ce88 /lib/random
parentc4fc9477aa5f77a69a40b619b7144f2b118b772e (diff)
downloadruby-7e5c662a6f2e8435f8103bc16185bed6759cc557.tar.gz
[Feature #18183] Add `chars:` option to `Random#alphanumeric`
Diffstat (limited to 'lib/random')
-rw-r--r--lib/random/formatter.rb13
1 files changed, 10 insertions, 3 deletions
diff --git a/lib/random/formatter.rb b/lib/random/formatter.rb
index 4dea61c16c..18e6717b6c 100644
--- a/lib/random/formatter.rb
+++ b/lib/random/formatter.rb
@@ -226,11 +226,13 @@ module Random::Formatter
#
# The argument _n_ specifies the length, in characters, of the alphanumeric
# string to be generated.
+ # The argument _chars_ specifies the character list which the result is
+ # consist of.
#
# If _n_ is not specified or is nil, 16 is assumed.
# It may be larger in the future.
#
- # The result may contain A-Z, a-z and 0-9.
+ # The result may contain A-Z, a-z and 0-9, unless _chars_ is specified.
#
# require 'random/formatter'
#
@@ -238,8 +240,13 @@ module Random::Formatter
# # or
# prng = Random.new
# prng.alphanumeric(10) #=> "i6K93NdqiH"
- def alphanumeric(n=nil)
+ #
+ # Random.alphanumeric(4, chars: [*"0".."9"])' #=> "2952"
+ # # or
+ # prng = Random.new
+ # prng.alphanumeric(10, chars: [*"!".."/"]) #=> ",.,++%/''."
+ def alphanumeric(n = nil, chars: ALPHANUMERIC)
n = 16 if n.nil?
- choose(ALPHANUMERIC, n)
+ choose(chars, n)
end
end