RubyでUUIDを生成したいならば、SecureRandomを使へばいい。SecureRandomはRubyに標準で入ってゐる。
cf. module SecureRandom http://docs.ruby-lang.org/ja/2.1.0/class/SecureRandom.html
require 'securerandom' puts SecureRandom.uuid # output => 2e895d5b-ab74-45de-9cee-67c8659d5aa0 puts SecureRandom.uuid # output => 0b8ef286-0d6c-41a9-9b48-b087fb5b565a
しかしActiveRecordをUUIDに対応させるlibrary、ActiveUUIDは、はUUIDToolsを使ってゐる。何が違ふのかは知らない。
cf. jashmenn/activeuuid https://github.com/jashmenn/activeuuid
cf. sporkmonger/uuidtools https://github.com/sporkmonger/uuidtools
ActiveUUIDの作った列からは、UUIDTools::UUID classのinstanceが返ってくる。
UUIDToolsを直接使う
gem install uuidtools
require 'uuidtools' uuid = UUIDTools::UUID.random_create # => #<UUID:0x4aa716c UUID:0e5bacd4-2636-4710-82ee-9f5a8d8d453c>
UUIDTools::UUID objectには、to_で始まるmethodが14個有る。
uuid.methods.select{|m| m.to_s =~ /^to_/ } # => [:to_s, :to_str, :to_i, :to_uri, :to_param, :to_query, :to_json_with_active_support_encoder, :to_json_without_active_support_encoder, :to_json, :to_yaml_properties, :to_yaml, :to_v8, :to_ruby, :to_enum]
全て試す。
puts uuid.to_s # output => 0e5bacd4-2636-4710-82ee-9f5a8d8d453c puts uuid.to_str # output => 0e5bacd4-2636-4710-82ee-9f5a8d8d453c p uuid.to_i # output => 19085196337763820606275207760219817276 puts uuid.to_uri # output => urn:uuid:0e5bacd4-2636-4710-82ee-9f5a8d8d453c puts uuid.to_param # output => 0E5BACD42636471082EE9F5A8D8D453C puts uuid.to_json_with_active_support_encoder # output => "0E5BACD42636471082EE9F5A8D8D453C" puts uuid.to_json_without_active_support_encoder # output => "0e5bacd4-2636-4710-82ee-9f5a8d8d453c" puts uuid.to_json # output => "0E5BACD42636471082EE9F5A8D8D453C" p uuid.to_yaml_properties # output => [:@time_low, :@time_mid, :@time_hi_and_version, :@clock_seq_hi_and_reserved, :@clock_seq_low, :@nodes, :@string, :@integer, :@hexdigest] puts uuid.to_yaml # output => # --- !ruby/object:UUIDTools::UUID # time_low: 240889044 # time_mid: 9782 # time_hi_and_version: 18192 # clock_seq_hi_and_reserved: 130 # clock_seq_low: 238 # nodes: # - 159 # - 90 # - 141 # - 141 # - 69 # - 60 # string: 0e5bacd4-2636-4710-82ee-9f5a8d8d453c # integer: 19085196337763820606275207760219817276 # hexdigest: 0e5bacd42636471082ee9f5a8d8d453c p uuid.to_ruby # output => #<UUID:0x4aa716c UUID:0e5bacd4-2636-4710-82ee-9f5a8d8d453c> p uuid.to_enum # output => #<Enumerator: #<UUID:0x4aa716c UUID:0e5bacd4-2636-4710-82ee-9f5a8d8d453c>:each>
ActiveUUIDのto_json値を得るには、to_param methodを使へば好い事が解る。"0E5BACD42636471082EE9F5A8D8D453C" の形式だ。
逆は、parse methodが有る。
p UUIDTools::UUID.parse '0e5bacd4-2636-4710-82ee-9f5a8d8d453c' # output => #<UUID:0x48242c8 UUID:0e5bacd4-2636-4710-82ee-9f5a8d8d453c> p UUIDTools::UUID.parse_raw '0e5bacd4-2636-4710-82ee-9f5a8d8d453c' # output => #<UUID:0x465cd78 UUID:0e5bacd4-2636-4710-82ee-9f5a8d8d453c> p UUIDTools::UUID.parse_int 19085196337763820606275207760219817276 # output => #<UUID:0x47054dc UUID:0e5bacd4-2636-4710-82ee-9f5a8d8d453c> p UUIDTools::UUID.parse_hexdigest 'E5BACD42636471082EE9F5A8D8D453C' # output => #<UUID:0x478ed2c UUID:0e5bacd4-2636-4710-82ee-9f5a8d8d453c> p UUIDTools::UUID.parse_string '0e5bacd4-2636-4710-82ee-9f5a8d8d453c' # output => #<UUID:0x46c78f8 UUID:0e5bacd4-2636-4710-82ee-9f5a8d8d453c>
UUIDTools::UUID.random_create.to_param
の逆は、UUIDTools::UUID.parse_hexdigest(hexdigest)
だと解る。
此れで使へる。
追記20140723:
UUIDTools::UUID.serialize(hexdigest)
でもよい。と云ふかわたしはこちらを先に発見してしたことを発見した。