Guid - Produce GUID/UUID from Ruby


require 'guid'

# generate a GUID
g =
puts g             # 79328095-636a-6cc5-2bbb-606df7228a01
puts g.to_s        # 79328095-636a-6cc5-2bbb-606df7228a01
puts g.hexdigest   # 79328095636a6cc52bbb606df7228a01
puts g.raw.inspect # "\227#\010Y6\246\306\\\262\273\006\326\177\"\250\020"
puts g.raw.length  # 16

# generate another GUID, should be different everytime
g2 =
puts g == g2       # false (it better be! :-)

# convert a hexstring into Guid object
g3 = Guid.from_s("79328095-636a-6cc5-2bbb-606df7228a01")
puts g3            # 79328095-636a-6cc5-2bbb-606df7228a01
puts g == g3       # true

# convert a raw 16-byte string into Guid object
g4 = Guid.from_raw("\227#\010Y6\246\306\\\262\273\006\326\177\"\250\020")
puts g4            # 79328095-636a-6cc5-2bbb-606df7228a01
puts g == g4       # true


This library can produce GUID/UUID on Windows (except first release of
Win95 and older version) and on Unix using random number. I have only
tested this library under Win2k and Redhat 7.3; please report if you fail
to use it on other platforms. On Windows, it uses CryptGenRandom(). On
Unix, it uses /dev/urandom (and if fail, try to use /dev/random). No other
external program or library is needed.

The latest version of this library can be obtained from:

To install:

  % ruby install.rb config
  % ruby install.rb setup
  # ruby install.rb install




* Thanks to Stephen Veit <sveit at earthlink net> for pointing out how to
  use Win32API.


Copyright (c) 2004 David Garamond <davegaramond at icqmail com>.

This library is free software; you can redistribute it and/or modify it
under the same terms as Ruby itself.