Skip to content

Instantly share code, notes, and snippets.

@potatosalad
Created December 22, 2011 17:36
Show Gist options
  • Save potatosalad/1511135 to your computer and use it in GitHub Desktop.
Save potatosalad/1511135 to your computer and use it in GitHub Desktop.
require 'digest/md5'
def bson_id
[
'%08x' % Time.now.utc.to_i, # 4 bytes
Digest::MD5.hexdigest(`hostname`.strip)[0..5], # 3 bytes
'%04x' % $$, # 2 bytes
'%06x' % rand(0..('f' * 6).to_i(16)) # 3 bytes
] # 12 bytes total
end
puts bson_id.inspect
puts bson_id.join
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment