diff options
Diffstat (limited to 'lib/random')
| -rw-r--r-- | lib/random/formatter.rb | 25 |
1 files changed, 13 insertions, 12 deletions
diff --git a/lib/random/formatter.rb b/lib/random/formatter.rb index 7fe7785e1a..4ecd6ad027 100644 --- a/lib/random/formatter.rb +++ b/lib/random/formatter.rb @@ -165,13 +165,13 @@ module Random::Formatter # # The result contains 122 random bits (15.25 random bytes). # - # See RFC4122[https://datatracker.ietf.org/doc/html/rfc4122] for details of UUID. + # See RFC9562[https://www.rfc-editor.org/rfc/rfc9562] for details of UUIDv4. # def uuid - ary = random_bytes(16).unpack("NnnnnN") - ary[2] = (ary[2] & 0x0fff) | 0x4000 - ary[3] = (ary[3] & 0x3fff) | 0x8000 - "%08x-%04x-%04x-%04x-%04x%08x" % ary + ary = random_bytes(16) + ary.setbyte(6, (ary.getbyte(6) & 0x0f) | 0x40) + ary.setbyte(8, (ary.getbyte(8) & 0x3f) | 0x80) + ary.unpack("H8H4H4H4H12").join(?-) end alias uuid_v4 uuid @@ -201,12 +201,10 @@ module Random::Formatter # # The result contains 74 random bits (9.25 random bytes). # - # Note that this method cannot be made reproducable with Kernel#srand, which - # can only affect the random bits. The sorted bits will still be based on - # Process.clock_gettime. + # Note that this method cannot be made reproducible because its output + # includes not only random bits but also timestamp. # - # See draft-ietf-uuidrev-rfc4122bis[https://datatracker.ietf.org/doc/draft-ietf-uuidrev-rfc4122bis/] - # for details of UUIDv7. + # See RFC9562[https://www.rfc-editor.org/rfc/rfc9562] for details of UUIDv7. # # ==== Monotonicity # @@ -243,7 +241,7 @@ module Random::Formatter # # Counters and other mechanisms for stronger guarantees of monotonicity are # not implemented. Applications with stricter requirements should follow - # {Section 6.2}[https://www.ietf.org/archive/id/draft-ietf-uuidrev-rfc4122bis-07.html#monotonicity_counters] + # {Section 6.2}[https://www.rfc-editor.org/rfc/rfc9562.html#name-monotonicity-and-counters] # of the specification. # def uuid_v7(extra_timestamp_bits: 0) @@ -293,6 +291,7 @@ module Random::Formatter end end + # Internal interface to Random; Generate random data _n_ bytes. private def gen_random(n) self.bytes(n) end @@ -340,7 +339,9 @@ module Random::Formatter result end - ALPHANUMERIC = [*'A'..'Z', *'a'..'z', *'0'..'9'] + # The default character list for #alphanumeric. + ALPHANUMERIC = [*'A'..'Z', *'a'..'z', *'0'..'9'].map(&:freeze).freeze + # Generate a random alphanumeric string. # # The argument _n_ specifies the length, in characters, of the alphanumeric |
