Class UUID
In: lib/uuid.rb
Parent: Object

Generating UUIDs

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

UUIDs in Brief

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.

Methods

Classes and Modules

Module UUID::Version
Class UUID::Client
Class UUID::Server

Constants

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.
:default:Produces 36 characters, including hyphens separating the UUID value parts
:compact:Produces a 32 digits (hexadecimal) value with no hyphens
:urn:Adds the prefix urn:uuid: to the default format
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.

Public Class methods

Generates a new UUID string using format. See FORMATS for a list of supported formats.

Returns the UUID generator used by generate. Useful if you need to mess with it, e.g. force next sequence when forking (e.g. Unicorn, Resque):

after_fork do

  UUID.generator.next_sequence

end

The access mode of the state file. Set it with state_file.

Create a new UUID generator. You really only need to do this once.

Call this to use a UUID Server. Expects address to bind to (SOCKET_NAME is 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).

Returns true if uuid is in compact, default or urn formats. Does not validate the layout (RFC 4122 section 4) of the UUID.

Public Instance methods

Generates a new UUID string using format. See FORMATS for a list of supported formats.

Uses system calls to get a mac address

return iee_mac_address if available, pseudo_mac_address otherwise

Updates the state file with a new sequence number.

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.

Protected Instance methods

Open the state file with an exclusive lock and access mode mode.

Read the state from io

Write that state to io

[Validate]