Class | UUID |
In: |
lib/uuid.rb
|
Parent: | Object |
Call generate to generate a new UUID. The method returns a string in one of three formats. The default format is 36 characters long, and contains the 32 hexadecimal octets and hyphens separating the various value parts. The :compact format omits the hyphens, while the :urn format adds the :urn:uuid prefix.
For example:
uuid = UUID.new 10.times do p uuid.generate end
UUID (universally unique identifier) are guaranteed to be unique across time and space.
A UUID is 128 bit long, and consists of a 60-bit time value, a 16-bit sequence number and a 48-bit node identifier.
The time value is taken from the system clock, and is monotonically incrementing. However, since it is possible to set the system clock backward, a sequence number is added. The sequence number is incremented each time the UUID generator is started. The combination guarantees that identifiers created on the same machine are unique with a high degree of probability.
Note that due to the structure of the UUID and the use of sequence number, there is no guarantee that UUID values themselves are monotonically incrementing. The UUID value cannot itself be used to sort based on order of creation.
To guarantee that UUIDs are unique across all machines in the network, the IEEE 802 MAC address of the machine‘s network interface card is used as the node identifier.
For more information see RFC 4122.
VERSION | = | Version::STRING | ||||||||
CLOCK_MULTIPLIER | = | 10000000 | Clock multiplier. Converts Time (resolution: seconds) to UUID clock (resolution: 10ns) | |||||||
CLOCK_GAPS | = | 100000 | Clock gap is the number of ticks (resolution: 10ns) between two Ruby Time ticks. | |||||||
VERSION_CLOCK | = | 0x0100 | Version number stamped into the UUID to identify it as time-based. | |||||||
FORMATS | = | { :compact => '%08x%04x%04x%04x%012x', :default => '%08x-%04x-%04x-%04x-%012x', :urn => 'urn:uuid:%08x-%04x-%04x-%04x-%012x', } |
Formats supported by the UUID generator.
|
|||||||
STATE_FILE_FORMAT | = | 'SLLQ' | MAC address (48 bits), sequence number and last clock | |||||||
SOCKET_NAME | = | "/var/lib/uuid.sock" | You don‘t have to use this, it‘s just a good default. |
Creates an empty state file in /var/tmp/ruby-uuid or the windows common application data directory using mode 0644. Call with a different mode before creating a UUID generator if you want to open access beyond your user by default.
If the default state dir is not writable, UUID falls back to ~/.ruby-uuid.
State files are not portable across machines.
Specify the path of the state file. Use this if you need a different location for your state file.
Set to false if your system cannot use a state file (e.g. many shared hosts).
Generate a pseudo MAC address because we have no pure-ruby way to know the MAC address of the NIC this system uses. Note that cheating with pseudo arresses here is completely legal: see Section 4.5 of RFC4122 for details.
This implementation is shamelessly stolen from
https://github.com/spectra/ruby-uuid/blob/master/uuid.rb
Thanks spectra.