From f2906bde2c3c85d913dcaa939c28f64ae7da0fc6 Mon Sep 17 00:00:00 2001 From: ksss Date: Sun, 20 Jul 2025 22:54:28 +0900 Subject: [PATCH 1/3] Drop undocumented override methods on Random::Formatter --- stdlib/securerandom/0/securerandom.rbs | 14 ----------- test/stdlib/SecureRandom_test.rb | 35 -------------------------- 2 files changed, 49 deletions(-) diff --git a/stdlib/securerandom/0/securerandom.rbs b/stdlib/securerandom/0/securerandom.rbs index 5b471d97f..5b7e86793 100644 --- a/stdlib/securerandom/0/securerandom.rbs +++ b/stdlib/securerandom/0/securerandom.rbs @@ -45,18 +45,4 @@ module SecureRandom # support Ruby 3.2 # def self.alphanumeric: (?Integer?) -> String - - def self.base64: (?Integer?) -> String - - def self.hex: (?Integer?) -> String - - def self.random_bytes: (?Integer?) -> String - - def self.random_number: () -> Float - | (Integer) -> Integer - | (Numeric) -> Numeric - - def self.urlsafe_base64: (?Integer?, ?bool?) -> String - - def self.uuid: () -> String end diff --git a/test/stdlib/SecureRandom_test.rb b/test/stdlib/SecureRandom_test.rb index 9cf4060e6..e007b5f0c 100644 --- a/test/stdlib/SecureRandom_test.rb +++ b/test/stdlib/SecureRandom_test.rb @@ -10,39 +10,4 @@ def test_alphanumeric SecureRandom.alphanumeric(nil) SecureRandom.alphanumeric(4) end - - def test_base64 - SecureRandom.base64 - SecureRandom.base64(nil) - SecureRandom.base64(4) - end - - def test_hex - SecureRandom.hex - SecureRandom.hex(nil) - SecureRandom.hex(3) - end - - def test_random_bytes - SecureRandom.random_bytes - SecureRandom.random_bytes(nil) - SecureRandom.random_bytes(3) - end - - def test_random_number - SecureRandom.random_number - SecureRandom.random_number(nil) - SecureRandom.random_number(0) - SecureRandom.random_number(4) - end - - def test_urlsafe_base64 - SecureRandom.urlsafe_base64 - SecureRandom.urlsafe_base64(nil, true) - SecureRandom.urlsafe_base64(24, false) - end - - def test_uuid - SecureRandom.uuid - end end From 070a1df9ada1c38ef3536467bf9d9be1dedf589f Mon Sep 17 00:00:00 2001 From: ksss Date: Sun, 20 Jul 2025 23:29:36 +0900 Subject: [PATCH 2/3] Refine `Random::Formatter#alphanumeric` * `SecureRandom.alphanumeric` has been removed. * Move to `Random::Formatter` * Add `chars` option. * Set documentation. * Add testing. --- core/rbs/unnamed/random.rbs | 30 ++++++++++++++++++++++++++ stdlib/securerandom/0/securerandom.rbs | 9 -------- test/stdlib/Random_test.rb | 18 ++++++++++++++++ test/stdlib/SecureRandom_test.rb | 6 ------ 4 files changed, 48 insertions(+), 15 deletions(-) diff --git a/core/rbs/unnamed/random.rbs b/core/rbs/unnamed/random.rbs index ec4dd7aab..71deef0f9 100644 --- a/core/rbs/unnamed/random.rbs +++ b/core/rbs/unnamed/random.rbs @@ -288,6 +288,36 @@ module RBS # %a{annotate:rdoc:copy:Random::Formatter#uuid} def uuid: () -> String + + # + # Generate a random alphanumeric string. + # + # 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, unless *chars* is specified. + # + # require 'random/formatter' + # + # Random.alphanumeric #=> "2BuBuLf3WfSKyQbR" + # # or + # prng = Random.new + # prng.alphanumeric(10) #=> "i6K93NdqiH" + # + # Random.alphanumeric(4, chars: [*"0".."9"]) #=> "2952" + # # or + # prng = Random.new + # prng.alphanumeric(10, chars: [*"!".."/"]) #=> ",.,++%/''." + # + %a{annotate:rdoc:copy:Random::Formatter#alphanumeric} + def alphanumeric: (?Numeric?, ?chars: Array[String]) -> String end end end diff --git a/stdlib/securerandom/0/securerandom.rbs b/stdlib/securerandom/0/securerandom.rbs index 5b7e86793..838fd85fc 100644 --- a/stdlib/securerandom/0/securerandom.rbs +++ b/stdlib/securerandom/0/securerandom.rbs @@ -36,13 +36,4 @@ # module SecureRandom extend Random::Formatter - - # - # Compatibility methods for Ruby 3.2, we can remove this after dropping to - # support Ruby 3.2 - # - def self.alphanumeric: (?Integer?) -> String end diff --git a/test/stdlib/Random_test.rb b/test/stdlib/Random_test.rb index 526f68d55..e7c826efd 100644 --- a/test/stdlib/Random_test.rb +++ b/test/stdlib/Random_test.rb @@ -38,6 +38,15 @@ def test_urandom assert_send_type "(::Integer) -> ::String", Random, :urandom, 0 end + + def test_alphanumeric + assert_send_type "() -> ::String", + Random, :alphanumeric + assert_send_type "(::Integer) -> ::String", + Random, :alphanumeric, 10 + assert_send_type "(::Integer, chars: Array[::String]) -> ::String", + Random, :alphanumeric, 10, chars: ["a", "b", "c"] + end end class RandomTest < Test::Unit::TestCase @@ -73,4 +82,13 @@ def test_seed assert_send_type "() -> ::Integer", Random.new, :seed end + + def test_alphanumeric + assert_send_type "() -> ::String", + Random.new, :alphanumeric + assert_send_type "(::Integer) -> ::String", + Random.new, :alphanumeric, 10 + assert_send_type "(::Integer, chars: Array[::String]) -> ::String", + Random.new, :alphanumeric, 10, chars: ["a", "b", "c"] + end end diff --git a/test/stdlib/SecureRandom_test.rb b/test/stdlib/SecureRandom_test.rb index e007b5f0c..d22de42e1 100644 --- a/test/stdlib/SecureRandom_test.rb +++ b/test/stdlib/SecureRandom_test.rb @@ -4,10 +4,4 @@ class SecureRandomTest < StdlibTest target SecureRandom library "securerandom" - - def test_alphanumeric - SecureRandom.alphanumeric - SecureRandom.alphanumeric(nil) - SecureRandom.alphanumeric(4) - end end From 602be7a30daa59da3579921e8e7c87b530f2ab30 Mon Sep 17 00:00:00 2001 From: ksss Date: Sun, 20 Jul 2025 23:33:32 +0900 Subject: [PATCH 3/3] Drop SecureRandom_test --- test/stdlib/SecureRandom_test.rb | 7 ------- 1 file changed, 7 deletions(-) delete mode 100644 test/stdlib/SecureRandom_test.rb diff --git a/test/stdlib/SecureRandom_test.rb b/test/stdlib/SecureRandom_test.rb deleted file mode 100644 index d22de42e1..000000000 --- a/test/stdlib/SecureRandom_test.rb +++ /dev/null @@ -1,7 +0,0 @@ -require_relative "test_helper" -require "securerandom" - -class SecureRandomTest < StdlibTest - target SecureRandom - library "securerandom" -end