class RUUID::UUID

Attributes

data[R]

Public Class Methods

new(data = RUUID.default_generator.generate) click to toggle source
# File lib/ruuid/uuid.rb, line 9
def initialize(data = RUUID.default_generator.generate)
  @data = normalize(data)
  validate
end
parse(string) click to toggle source
# File lib/ruuid/uuid.rb, line 5
def self.parse(string)
  new(Parser.parse(string))
end

Public Instance Methods

<=>(other) click to toggle source
Calls superclass method
# File lib/ruuid/uuid.rb, line 59
def <=>(other)
  return super unless other.respond_to?(:data)
  data <=> other.data
end
==(other) click to toggle source
Calls superclass method
# File lib/ruuid/uuid.rb, line 48
def ==(other)
  return super unless other.respond_to?(:data)
  data == other.data
end
Also aliased as: eql?
===(other) click to toggle source
Calls superclass method
# File lib/ruuid/uuid.rb, line 54
def ===(other)
  return super unless other.respond_to?(:to_str)
  to_str === other.to_str
end
as_json(*) click to toggle source
# File lib/ruuid/uuid.rb, line 21
def as_json(*)
  to_s
end
eql?(other)
Alias for: ==
hash() click to toggle source
# File lib/ruuid/uuid.rb, line 44
def hash
  data.hash
end
inspect() click to toggle source
# File lib/ruuid/uuid.rb, line 29
def inspect
  "<#{self.class}:0x#{object_id} data=#{to_s}>"
end
marshal_dump() click to toggle source

@private

# File lib/ruuid/uuid.rb, line 34
def marshal_dump
  data
end
marshal_load(data) click to toggle source

@private

# File lib/ruuid/uuid.rb, line 39
def marshal_load(data)
  @data = data.dup.freeze
  validate
end
to_json(*args) click to toggle source
# File lib/ruuid/uuid.rb, line 25
def to_json(*args)
  to_s(*args).to_json
end
to_s(format = RUUID.default_format) click to toggle source
# File lib/ruuid/uuid.rb, line 16
def to_s(format = RUUID.default_format)
  Formatter.format(data, format)
end
Also aliased as: to_str
to_str(format = RUUID.default_format)
Alias for: to_s
version() click to toggle source
# File lib/ruuid/uuid.rb, line 64
def version
  data.bytes[6] >> 4
end

Private Instance Methods

normalize(data) click to toggle source
# File lib/ruuid/uuid.rb, line 80
def normalize(data)
  data.dup.force_encoding(Encoding::BINARY).freeze
end
validate() click to toggle source
# File lib/ruuid/uuid.rb, line 70
def validate
  unless data.length == 16
    raise InvalidUUIDError, "Invalid UUID length: #{data.inspect}"
  end

  unless data.bytes[8] >> 6 == 2
    raise InvalidUUIDError, "Invalid UUID variant: #{data.inspect}"
  end
end