Ruby on Rails - creating the u.condition style bit.ly

I am trying to create a UUID with the same style as bit.ly, for example:

http://bit.ly/aUekJP

or cloudapp:

http://cl.ly/1hVU

which are even smaller

How can i do this? I am now using the UUID pearl for ruby, but I'm not sure if it is possible to limit the length and get something like this. I am currently using this:

UUID.generate.split("-")[0] => b9386070

But I would like to have even less and knowing that it will be unique.

Any help would be greatly appreciated :)

+5
source share
4 answers

. UUID . , . 36 . 8 , .

, tinyurl et-al . URL- , , URL-. UUIDS.

, , , , , , base32, 10 22 , , , "o" "i" ..

base32 gem, Douglas Crockford 32

Base32 5 33 6- .

+14

,

6175601989.to_s(30)
 => "8e45ttj" 

"8e45ttj".to_i(30)
=>6175601989

, short_code.

, , : 1lji0o. , . , , , , , . , "", , , .

+10

, :

def create_uuid(prefix=nil)
  time   = (Time.now.to_f * 10_000_000).to_i
  jitter = rand(10_000_000) 
  key    = "#{jitter}#{time}".to_i.to_s(36)
  [prefix, key].compact.join('_')
end

, : " 3qaishe3gpp07w2m"
"", .

Protest: This is not guaranteed to be unique (use SecureRandom.uuid for this), but it is very reliable:

10_000_000.times.map {create_uuid}.uniq.length == 10_000_000
0
source

The only way to guarantee uniqueness - to keep global account and increase it for everyone to use: 0000, 0001etc.

-10
source

All Articles