lib/uuid.rb
def generate(format = :default)
    template = FORMATS[format]

    raise ArgumentError, "invalid UUID format #{format.inspect}" unless template

    # The clock must be monotonically increasing. The clock resolution is at
    # best 100 ns (UUID spec), but practically may be lower (on my setup,
    # around 1ms). If this method is called too fast, we don't have a
    # monotonically increasing clock, so the solution is to just wait.
    #
    # It is possible for the clock to be adjusted backwards, in which case we
    # would end up blocking for a long time. When backward clock is detected,
    # we prevent duplicates by asking for a new sequence number and continue
    # with the new clock.

    clock = @mutex.synchronize do
      clock = (Time.new.to_f * CLOCK_MULTIPLIER).to_i & 0xFFFFFFFFFFFFFFF0

      if clock > @last_clock then
        @drift = 0
        @last_clock = clock
      elsif clock == @last_clock then
        drift = @drift += 1

        if drift < 10000 then
          @last_clock += 1
        else
          Thread.pass
          nil
        end
      else
        next_sequence
        @last_clock = clock
      end
    end until clock

    template % [
        clock        & 0xFFFFFFFF,
       (clock >> 32) & 0xFFFF,
      ((clock >> 48) & 0xFFFF | VERSION_CLOCK),
      @@sequence      & 0xFFFF,
      @mac           & 0xFFFFFFFFFFFF
    ]
  end
##
# Generates a new UUID string using +format+.  See FORMATS for a list of
# supported formats.
Generates a new UUID string using +format+.  See FORMATS for a list of
supported formats.
Generates a new UUID string using + format + . See FORMATS for a list of supported formats .
def generate(format = :default)
    template = FORMATS[format]
    raise ArgumentError, "invalid UUID format {format.inspect}" unless template
     It is possible for the clock to be adjusted backwards, in which case we
     would end up blocking for a long time. When backward clock is detected,
     we prevent duplicates by asking for a new sequence number and continue
     with the new clock.
    clock = @mutex.synchronize do
      clock = (Time.new.to_f * CLOCK_MULTIPLIER).to_i & 0xFFFFFFFFFFFFFFF0
      if clock > @last_clock then
        @drift = 0
        @last_clock = clock
      elsif clock == @last_clock then
        drift = @drift += 1
        if drift < 10000 then
          @last_clock += 1
        else
          Thread.pass
          nil
        end
      else
        next_sequence
        @last_clock = clock
      end
    end until clock
    template % [
        clock        & 0xFFFFFFFF,
       (clock >> 32) & 0xFFFF,
      ((clock >> 48) & 0xFFFF | VERSION_CLOCK),
      @@sequence      & 0xFFFF,
      @mac           & 0xFFFFFFFFFFFF
    ]
  end
def generate ( format = :default ) template = FORMATS [ format ] raise ArgumentError , "invalid UUID format #{format.inspect}" unless template clock = @mutex . synchronize do clock = ( Time . new . to_f * CLOCK_MULTIPLIER ) . to_i & 0xFFFFFFFFFFFFFFF0 if clock > @last_clock then @drift = 0 @last_clock = clock elsif clock == @last_clock then drift = @drift += 1 if drift < 10000 then @last_clock += 1 else Thread . pass nil end else next_sequence @last_clock = clock end end until clock template % [ clock & 0xFFFFFFFF , ( clock >> 32 ) & 0xFFFF , ( ( clock >> 48 ) & 0xFFFF | VERSION_CLOCK ) , @@sequence & 0xFFFF , @mac & 0xFFFFFFFFFFFF ] end
Generates a new UUID string using +format+.
Generates a new UUID string using + format + .
# The clock must be monotonically increasing. The clock resolution is at # best 100 ns (UUID spec), but practically may be lower (on my setup, # around 1ms). If this method is called too fast, we don't have a # monotonically increasing clock, so the solution is to just wait. # # It is possible for the clock to be adjusted backwards, in which case we # would end up blocking for a long time. When backward clock is detected, # we prevent duplicates by asking for a new sequence number and continue # with the new clock.
format