class UUID
Constants
Public Class Methods
new(v)
click to toggle source
Create UUID
@param [String,Integer] UUID
value
@raise [ArgumentError] given integer is too big or
given string is not 16-byte 8bit
# File lib/uuid.rb, line 49 def initialize(v) case v when Integer hex = "%032x" % [ v ] if hex.size > 32 raise ArgumentError, "integer to big (must fit in 128-bit)" end @raw = [ hex ].pack('H32') when String if v.size != 16 || v.encoding.name != 'ASCII-8BIT' raise ArgumentError, "need to be a 16 byte ASCII-8BIT string (#{v})" end @raw = v.dup.freeze else raise ArgumentError, "expected 128-bit integer or 16-byte string" end end
parse(str)
click to toggle source
Parse a UUID
formatted string
@param str [String] string to parse
@raise [ArgumentError] the string is not parsable as a UUID
@return [UUID]
# File lib/uuid.rb, line 33 def self.parse(str) unless str =~ REGEX raise ArgumentError, "unable to parse UUID value" end self.new( [str.delete('-')].pack('H32') ) end
Public Instance Methods
===(other)
click to toggle source
# File lib/uuid.rb, line 87 def ===(other) other = UUID(other) rescue nil !other.nil? && self.to_raw == other.to_raw end
bytes()
click to toggle source
Return the 16-byte sequence forming the UUID
.
@return [Array<Integer>]
# File lib/uuid.rb, line 75 def bytes @raw.bytes end
eql?(other)
click to toggle source
# File lib/uuid.rb, line 91 def eql?(other) !other.nil? && other.is_a?(UUID) && self.to_raw == other.to_raw end
Also aliased as: ==
hash()
click to toggle source
Generates an integer hash value
@return [Integer] hash value
# File lib/uuid.rb, line 84 def hash @raw.hash end
inspect()
click to toggle source
Returns a string containing a human-readable representation of this object
@return [String]
# File lib/uuid.rb, line 103 def inspect "#<#{self.class}:#{to_s}>" end
sql_literal(ds)
click to toggle source
Sequel
# File lib/uuid.rb, line 148 def sql_literal(ds) ; '0x' + @raw.unpack('H32')[0] ; end
to_blob()
click to toggle source
# File lib/uuid.rb, line 149 def to_blob ; Sequel.blob(@raw) ; end
to_i()
click to toggle source
Get the UUID
value as an integer
@return [Integer] UUID
value
# File lib/uuid.rb, line 112 def to_i i, j = @raw.unpack('Q>Q>') i * 2**64 + j end
Also aliased as: to_int
to_json(*a)
click to toggle source
Get the JSON represenation
@return [String]
# File lib/uuid.rb, line 142 def to_json(*a) self.to_s.to_json(*a) end
to_raw()
click to toggle source
# File lib/uuid.rb, line 96 def to_raw ; @raw ; end
to_s()
click to toggle source
Get the UUID
string representation
@return [String] UUID
string representation
# File lib/uuid.rb, line 123 def to_s @raw.unpack('H8H4H4H4H12').join('-') end
Also aliased as: to_str
to_uri()
click to toggle source
Get the URI has a URN in UUID
namespace
@return [String] Uniform Resource Name
# File lib/uuid.rb, line 133 def to_uri "urn:uuid:#{self}" end