1. Oct 18th, 2005

    Ruby UUID Generator

    Because every identifier wants to feel special. When you’re building a system that moves data around between different components, one of the first problems to tackle are how do you make sure all identifiers are unique. You can use sequences inside the code, but identifiers stop being unique when the application restarts. You can use auto increment keys in the database, but that only works if the data is tied to one specific table. There are hacks like using pre-allocated sequences backed by a file, or a sequence table in the database. But all tend to get a little complicated when you want to not worry about how the identifiers are generated, or who gets used to them. I just prefer using UUIDs. UUIDs are guaranteed to be “unique across time and space”, and with a 128-bit space, that’s not a big problem. The time-based algorithm I use relies on the system clock to generate new identifiers that are never repeated, and appends a sequence number to deal with two (or more) applications seeding from the same machine, or the system clock being set backwards. Each UUID also includes an identifier unique to that machine, using the network card’s MAC address, a 47-bit value that’s individually assigned to each network card. A UUID looks something like this:

    p UUID.new » "5158feb0-2293-0128-163b-080e46214b35"p UUID.new » "ff2c0c80-2293-0128-163c-080e46214b35"

    So feel free to get the code and play with it. And, if you have a quick and simple way for extracting a MAC address in Ruby, I’d like to integrate that as well.

    1. May 17th, 2006

      Ken Anderson

      I was looking for a UUID generator for Ruby and so was excited to find this blog post. Unfortunately, the URL above returns a 404 Not Found error message. Is the code still available?

      Thanks!

      Ken

    2. May 17th, 2006

      Assaf

      Sorry, I moved the code around in the repository. The correct URL is:
      http://trac.labnotes.org/cgi-bin/trac.cgi/browser/uuid

    3. Aug 19th, 2006

      itsnotvalid

      Is 1.0.2 going to be available in rubygems?
      I got the gem from rubyforge, and it is still 1.0.1.

    4. Aug 19th, 2006

      Assaf

      I just uploaded a 1.0.2 release to RubyForge, so gem update will get you the new one.

    5. Aug 24th, 2006

      stephen tudor

      Thanks so much for the great gem. It works wonderfully for me and it’s perfect for the project I’m working on.

    6. Aug 25th, 2006

      Assaf

      Thanks, Stephen.

    7. Nov 5th, 2006

      ebuprofen

      Hello, I use this gem and I like it but I found a bug

      If in the mac Address we have only decimal letter (example
      00:01:13:12:52:65) YAML return a number and UUID return a format exception

      Bye

    8. Nov 8th, 2006

      Assaf

      Thanks, I just uploaded a new release (1.0.3) that will fix this.

    9. Nov 30th, 2006

      Sidney Cammeresi

      Would it be possible to modify this so that I could either specify a statefile path to UUID.setup or have UUID.config automatically detect my MAC address?

      Right now it is either-or: autodetect requires write access to the gem directory whereas a custom statefile location doesn’t have any provision for autodetect. I want autodetect && no write access to gem dir && custom statefile location.

    10. Nov 30th, 2006

      Assaf

      Sidney,

      I’m going to remove the code that attempts to put the UUID state file in the Gems directory. It only works well when I originally used UUID inside an app, not as a Gem.

      Then you can run uuid_setup to create a default uuid.state file, place it anywhere you want and have UUID point at it.

      Expect a new release within a few days.

    11. Apr 7th, 2007

      Corey Jewett

      Of course I can no longer reproduce the UUID problem. Very weird. I even turned fastthread on and off, but everything seems happy now. I don’t think fastthread was enabled in the first place. Regardless, ignore my last.

    12. Apr 11th, 2008

      Brian Meidell

      Is there any chance you’d consider storing the uuid.state file in the temp dir or somewhere besides the current dir?
      Also, any chance you could make uuid default to not print out status messages on every invoke?

    13. Apr 11th, 2008

      Assaf

      You can call UUID.config and tell it where the state file is located.

      The state file is used to record the node ID and a sequence number that gets incremented each time you start the application, to prevent duplicate IDs. It can be anywhere but ideally not in a temp directory because you want it to stick around.

    14. Oct 10th, 2008

      Chris Bloom

      I’m currently running the uuid-2.0.1 gem, which worked before upgrading, but no longer. My app keeps erroring out with the message:

      “can’t convert UUID into String”

      Where do I call uuid-setup from? I tried it from my app root, but get a message saying:

      “/usr/bin/uuid-setup:19:in `load’: no such file to load — uuid-setup”
      “(LoadError) from /usr/bin/uuid-setup:19″

      Any help would be appreciated.

    15. Oct 10th, 2008

      Assaf

      UUID 2.0 is not just better but also different, uuid-setup is gone, and API changed a bit. The documentation is now hosted on Github.

    16. Oct 28th, 2008

      Anonymous

      OK, so I’m using 2.0 now, and it’s working OK on my production Linux box. But on my Windows development machine it keeps erring with this message:

      ArgumentError in LoginController#signin

      couldn’t find HOME environment — expanding `~/.ruby-uuid’

      RAILS_ROOT: ./script/../config/..

      Application Trace | Framework Trace | Full Trace

      C:/InstantRails/ruby/lib/ruby/gems/1.8/gems/uuid-2.0.1/lib/uuid.rb:147:in `expand_path’
      C:/InstantRails/ruby/lib/ruby/gems/1.8/gems/uuid-2.0.1/lib/uuid.rb:147:in `state_file’
      C:/InstantRails/ruby/lib/ruby/gems/1.8/gems/uuid-2.0.1/lib/uuid.rb:160:in `initialize’
      C:/InstantRails/ruby/lib/ruby/gems/1.8/gems/uuid-2.0.1/lib/uuid.rb:113:in `new’
      C:/InstantRails/ruby/lib/ruby/gems/1.8/gems/uuid-2.0.1/lib/uuid.rb:113:in `generate’
      C:/InstantRails/rails_apps/lifespan/app/models/contact.rb:113:in `remember_me’
      C:/InstantRails/rails_apps/lifespan/app/controllers/login_controller.rb:15:in `signin’
      -e:2:in `load’
      -e:2

      I’ve added permissions to the C:\Documents and Settings\All Users\Application Data folder so that Everyone has modify (read/write/exec) access, but it still fails. I even created the file manually, but still the error. I take it there is also no longer a uuid.config option that can be used to set the path to the uuid file?

    17. Nov 17th, 2008

      Devon

      I am getting the same error as the person above on my Windows development machine.

      ArgumentError: couldn’t find HOME environment — expanding `~/.ruby-uuid
      - this is using 2.0, I have the older version working just fine on a test server

    18. Nov 19th, 2008

      Assaf

      I don’t right now have a Windows setup to test this. I am working on that, but meanwhile if anyone can provide a patch.

    19. Dec 8th, 2008

      Alexey

      In windows you can easily do the trick with HOME system environment variable.

      from command prompt:
      set HOME=c:\home

      But better:

      right click on My Computer > Advanced tab > Environment variables button
      then add “New variable” with name=”HOME” and value=”c:\home”
      OK > OK

      you’re done.

      p.s. dont forget to create “c:\home” dir.
      p.s.s. can replace “c:\home” with anything u want.

      if you running “ruby server/start” from command prompt reopen it (command promt) to “reload” environment vars..

      It worked for me.

      Regards,

    Your comment, here ⇓