Ruby on Rails - 生成 bit.ly 样式标识符 [英] Ruby on Rails - generating bit.ly style identifiers

查看:70
本文介绍了Ruby on Rails - 生成 bit.ly 样式标识符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试生成与 bit.ly 网址样式相同的 UUID,例如:

I'm trying to generate UUIDs with the same style as bit.ly urls like:

http://bit [dot] ly/aUekJP

或云应用程序:

http://cl [dot] ly/1hVU

哪个更小

我该怎么做?我现在将 UUID gem 用于 ruby​​,但我不确定是否可以限制长度并获得类似的结果.我目前正在使用这个:

how can I do it? I'm now using UUID gem for ruby but I'm not sure if it's possible to limitate the length and get something like this. I am currently using this:

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

但我想要更小并且知道它会是独一无二的.

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

非常感谢任何帮助:)

编辑说明:用 [dot] 替换点字母以解决 禁止短链接

edit note: replaced dot letters with [dot] for workaround of banned short link

推荐答案

你在这里混淆了两个不同的东西.UUID 是通用唯一标识符.即使同时在世界各地创建数百万个,它也很有可能是独一无二的.它通常显示为 36 位字符串.您不能砍掉前 8 个字符并期望它是唯一的.

You are confusing two different things here. A UUID is a universally unique identifier. It has a very high probability of being unique even if millions of them were being created all over the world at the same time. It is generally displayed as a 36 digit string. You can not chop off the first 8 characters and expect it to be unique.

有点,tinyurl 等存储链接并生成一个短代码来表示该链接.它们不会根据在数据存储中查找的代码重建 URL 并返回相应的 URL.这些不是 UUIDS.

Bitly, tinyurl et-al store links and generate a short code to represent that link. They do not reconstruct the URL from the code they look it up in a data-store and return the corresponding URL. These are not UUIDS.

在不了解您的应用程序的情况下,很难建议您应该使用哪种方法,但是您可以使用数字键将您指向的任何内容存储在数据存储中,然后使用 10 位数字和 22 将密钥重新设置为 base32小写字母,或许可以避免明显的拼写错误,例如 'o' 'i' 'l' 等

Without knowing your application it is hard to advise on what method you should use, however you could store whatever you are pointing at in a data-store with a numeric key and then rebase the key to base32 using the 10 digits and 22 lowercase letters, perhaps avoiding the obvious typo problems like 'o' 'i' 'l' etc

编辑

在进一步调查中,有一个 Ruby base32 gem 可用于实现 Douglas Crockford 的 Base 32 实现

On further investigation there is a Ruby base32 gem available that implements Douglas Crockford's Base 32 implementation

一个 5 个字符的 Base32 字符串可以表示超过 3300 万个整数,一个 6 位字符串可以表示超过 10 亿个.

A 5 character Base32 string can represent over 33 million integers and a 6 digit string over a billion.

这篇关于Ruby on Rails - 生成 bit.ly 样式标识符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆