Ruby uuidtools

要使用UUID,把使用方法总结如下:

Example usage

require "uuidtools"

UUIDTools::UUID.md5_create(UUIDTools::UUID_DNS_NAMESPACE, "www.widgets.com")
# => #<UUID:0x287576 UUID:3d813cbb-47fb-32ba-91df-831e1593ac29>
UUIDTools::UUID.sha1_create(UUIDTools::UUID_DNS_NAMESPACE, "www.widgets.com")
# => #<UUID:0x2a0116 UUID:21f7f8de-8051-5b89-8680-0195ef798b6a>
UUIDTools::UUID.timestamp_create
# => #<UUID:0x2adfdc UUID:64a5189c-25b3-11da-a97b-00c04fd430c8>
UUIDTools::UUID.random_create
# => #<UUID:0x19013a UUID:984265dc-4200-4f02-ae70-fe4f48964159>

Install

$ sudo gem install uuidtools

 

安装

gem install uuidtools
gem install uuidtools -v=2.1.3
也可以在Gemfile里加上
gem 'uuidtools'

使用方法

UUIDTools::UUID.md5_create(UUIDTools::UUID_DNS_NAMESPACE, "www.rubyer.me")
=> #<UUID:0x287576 UUID:3d813cbb-47fb-32ba-91df-831e1593ac29>
UUIDTools::UUID.sha1_create(UUIDTools::UUID_DNS_NAMESPACE, "www.rubyer.me")
=> #<UUID:0x2a0116 UUID:21f7f8de-8051-5b89-8680-0195ef798b6a>
UUIDTools::UUID.timestamp_create
=> #<UUID:0x2adfdc UUID:64a5189c-25b3-11da-a97b-00c04fd430c8>
UUIDTools::UUID.random_create
=> #<UUID:0x19013a UUID:984265dc-4200-4f02-ae70-fe4f48964159>

如果想去掉中间的-
可以这样

new_name = UUIDTools::UUID.timestamp_create.to_s.gsub('-','')

官方地址:
http://uuidtools.rubyforge.org/
https://github.com/sporkmonger/uuidtools/tree/

如果安装不了,就直接去官网下载把lib里的东西copy到项目的lib下
如果还是不行就把下载的包拷贝到vendor\plugins下
就可以直接使用了,不需要require

posted @ 2012-08-24 11:30  残星  阅读(1086)  评论(0编辑  收藏  举报