Skip to content

Instantly share code, notes, and snippets.

@sporkmonger
Created December 1, 2008 21:32
Show Gist options
  • Save sporkmonger/30856 to your computer and use it in GitHub Desktop.
Save sporkmonger/30856 to your computer and use it in GitHub Desktop.
UUID # a wrapper class for UUIDs, not a generator, with handy implicit to_str cast
UUID::Generator
UUID::TimestampGenerator.new
UUID::RandomGenerator.new
UUID::SHA1Generator.new(namespace)
UUID::MD5Generator.new(namespace)
UUID::Generator::DEFAULT = UUID::TimestampGenerator.new
uuid = UUID.new
#=> #<UUID:0xdb5268 UUID:012c377a-bfeb-11dd-b84a-001ec2186a45>
uuid.hexdigest # aliases: compact
"012c377abfeb11ddb84a001ec2186a45"
uuid.to_s # aliases: to_str
#=> "012c377a-bfeb-11dd-b84a-001ec2186a45"
uuid.to_i
#=> 1558814315320294684408074049578101317
UUID.generate
#=> "ff38b840-a218-012b-64f4-001ec2186a45"
UUID.generate(:generator => :timestamp, :format => :hexdigest)
#=> "f9fca3d0a218012b64f4001ec2186a45"
UUID.generate(:generator => :timestamp, :format => :base64)
#=> "ASw3er/rEd24SgAewhhqRQ=="
# Used when the UUID may appear in a URI path
UUID.generate(:generator => :timestamp, :format => :unreserved)
#=> "abldD6V-Sr3BHkab7cggPf"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment