diff options
author | Nobuyoshi Nakada <nobu@ruby-lang.org> | 2023-08-29 10:56:56 +0900 |
---|---|---|
committer | Nobuyoshi Nakada <nobu@ruby-lang.org> | 2023-08-29 10:56:56 +0900 |
commit | 7e5c662a6f2e8435f8103bc16185bed6759cc557 (patch) | |
tree | c8e6fb7cbc431e59400cf6f9200a335462b4ce88 /lib | |
parent | c4fc9477aa5f77a69a40b619b7144f2b118b772e (diff) | |
download | ruby-7e5c662a6f2e8435f8103bc16185bed6759cc557.tar.gz |
[Feature #18183] Add `chars:` option to `Random#alphanumeric`
Diffstat (limited to 'lib')
-rw-r--r-- | lib/random/formatter.rb | 13 |
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 |