无意间发现一UUID生成器UUIDTools:http://rubyforge.org/projects/uuidtools/,可以从这里下载http://rubyforge.org/frs/?group_id=914。它本身就是个plugin,所以并不需要我们做什么额外的事情,只需要放到vendorpluginsuuidtools下,然后在需要用的地方require进来就可以了
ex:
def getFileName(filename)
if !filename.nil?
require 'uuidtools'
filename.sub(/.*./,UUID.random_create.to_s+'.')
end
end
readme.txt中的使用说明
UUID.md5_create(UUID_DNS_NAMESPACE, "www.widgets.com")
=> #<UUID:0x287576 UUID:3d813cbb-47fb-32ba-91df-831e1593ac29>
UUID.sha1_create(UUID_DNS_NAMESPACE, "www.widgets.com")
=> #<UUID:0x2a0116 UUID:21f7f8de-8051-5b89-8680-0195ef798b6a>
UUID.timestamp_create
=> #<UUID:0x2adfdc UUID:64a5189c-25b3-11da-a97b-00c04fd430c8>
UUID.random_create
=> #<UUID:0x19013a UUID:984265dc-4200-4f02-ae70-fe4f48964159>
看一下它的测试用类:
require 'test/unit'
require 'uuidtools'
class CreateTest < Test::Unit::TestCase
def setup
end
def test_sha1_create
assert_equal(
"f2d04685-b787-55da-8644-9bd28a6f5a53",
UUID.sha1_create(UUID_URL_NAMESPACE, 'http://sporkmonger.com').to_s)
end
def test_md5_create
assert_equal(
"15074785-9071-3fe3-89bd-876e4b9e919b",
UUID.md5_create(UUID_URL_NAMESPACE, 'http://sporkmonger.com').to_s)
end
def test_timestamp_create
assert_not_equal(
UUID.timestamp_create.to_s,
UUID.timestamp_create.to_s)
current_time = Time.now
assert_not_equal(
UUID.timestamp_create(current_time).to_s,
UUID.timestamp_create(current_time).to_s)
uuids = []
1000.times do
uuids << UUID.timestamp_create
end
assert_equal(uuids.size, (uuids.map {|x| x.to_s}).uniq.size,
"Duplicate timestamp-based UUID generated.")
end
def test_random_create
assert_not_equal(
UUID.random_create.to_s,
UUID.random_create.to_s)
end
end
- 描述: UUIDTools 生成的UUID
- 大小: 31.7 KB
分享到:
相关推荐
shoppe-example, 在 Rails 中使用Shoppe平台的示例存储实现 这是一个 Rails 应用程序,它使用 Shoppe 构建。 它具有一个可以爱的设计,以充分演示Shoppe平台提供的功能以及它在 Rails 应用程序中的。 正在启动要开始...
NULL 博文链接:https://hlee.iteye.com/blog/587000
NULL 博文链接:https://hlee.iteye.com/blog/345775
mail_form, 在 Rails 中使用 i18n 验证附件和请求信息,直接从表单发送电子邮件 MailForm Rails 3这个 gem 构建在 ActiveModel 之上,展示如何从 Rails 中提取验证。命名和 i18n,而不需要自己实现。本自述文件指的...
rails-assets, 在 Rails 中,资产管理的解决 Rails 资产 Bundler 到 Bower 代理本自述文件涉及项目的开发方面。 访问站点了解如何在你的应用程序中使用 Rails 资产。 插件开发设置git clone git@github.com:tenex/r
Ruby on Rails中文指南
这是一个在Rails应用程序中使用React并使用webpack的示例
rails_email_preview, 在 Rails 中,预览和编辑应用程序邮件程序模板 Rails 电子邮件预览 使用这里 Rails 引擎在浏览器中预览电子邮件。 兼容 Rails 4.2 。电子邮件审阅: 所有电子邮件预览的列表: 代表有两个主题...
Webpacker:使用Webpack在Rails中管理类似app的JavaScript模块
使用Aptana+Rails开发Rails Web应用 有Aptana的安装配置等等,中文
博文链接:https://ziyoujiedao.iteye.com/blog/148307
Ruby On Rails中文教材(PDF)
使您在Rails应用程序中使用Roar的代表变得有趣。 咆哮轨道的开发将在未来停止,我们鼓励用户开始迁移到 (和 )。 Roar是用于解析和呈现REST文档的框架。 有关代表的更好的概述,请检查。 Roar-rails可为您提供...
rails, Ruby on Rails 欢迎使用 RailsRails 是一个web应用程序框架,它包括根据 Model-View-Controller ( MVC ) Pattern 创建数据库备份的web应用程序所需的所有内容。理解 MVC Pattern 是理解 Rai
使用引导模式运行的 Rails 4 应用程序! 这个应用程序展示了如何在 rails 4 中使用模态请参阅 users/index.html.erb 中的 Users#Show 按钮
backbone-rails, 在 Rails 3.1中轻松使用 backbone.js 主干 rails 轻松设置并使用 Rails 3.1和更高版本的backbone.js ( 1.2.2 )##Version#####Github 主 branch###gem 版本:1.2.2Backbone 版本:1.
敏捷Rails中文教程 敏捷Rails中文教程 敏捷Rails中文教程
在过去的几年中,《Ruby on Rails Tutorial》这本书被视为介绍使用 Rails 进行 Web 开发的先驱者。 在这个全球互联的世界中,计算机编程和 Web 应用程序开发都在迅猛发展,我很期待能为中国的开发者提供 Ruby on ...