Generate UUID in Ruby

Table of Contents:

  1. Introduction
  2. Method 1: Using the SecureRandom Library
  3. Method 2: Using the UUIDTools Gem
  4. Method 3: Generating UUID From a String
  5. Method 4: Using the SecureRandom Library with UUID Formatting
  6. Conclusion
  7. References

Introduction

UUID (Universally Unique Identifier) is a unique identifier that is used to identify information in computer systems. In Ruby, there are several different ways to generate a UUID. In this tutorial, we will explore four different methods for generating UUID in Ruby and provide examples for each method.

Method 1: Using the SecureRandom Library

The SecureRandom library in Ruby provides a secure way to generate random numbers and strings. To generate a UUID using the SecureRandom library, you can use the following code:

require 'securerandom'

uuid = SecureRandom.uuid
puts uuid

This code imports the SecureRandom library and calls the uuid method to generate a random UUID. The generated UUID is then printed to the console.

Method 2: Using the UUIDTools Gem

The UUIDTools gem is a library that provides utilities for generating and manipulating UUIDs. To generate a UUID using the UUIDTools gem, you can use the following code:

require 'uuidtools'

uuid = UUIDTools::UUID.random_create
puts uuid

This code imports the UUIDTools library and calls the random_create method to generate a random UUID. The generated UUID is then printed to the console.

Method 3: Generating UUID From a String

You can also generate a UUID from a string using the Digest library in Ruby. To generate a UUID from a string, you can use the following code:

require 'digest'

uuid = Digest::UUID.uuid_v5(Digest::UUID::DNS_NAMESPACE, 'example.com')
puts uuid

This code imports the Digest library and uses the uuid_v5 method to generate a UUID from the given string and a namespace. In this example, we use the DNS namespace and the string 'example.com'. The generated UUID is then printed to the console.

Method 4: Using the SecureRandom Library with UUID Formatting

If you need to generate a formatted UUID with hyphens, you can use the SecureRandom library in combination with the insert method. Here is an example:

require 'securerandom'

uuid = SecureRandom.uuid.insert(8, '-').insert(13, '-')
puts uuid

This code generates a UUID using the SecureRandom library and then inserts hyphens at the 8th and 13th positions to format the UUID. The resulting formatted UUID is then printed to the console.

Conclusion

In this tutorial, we explored four different methods for generating UUID in Ruby. We covered using the SecureRandom library, the UUIDTools gem, generating UUID from a string, and generating formatted UUID with the SecureRandom library. You can choose the method that best fits your needs based on your requirements.

References

1. SecureRandom Library - https://ruby-doc.org/stdlib-2.6.3/libdoc/securerandom/rdoc/SecureRandom.html

2. UUIDTools Gem - https://rubygems.org/gems/uuidtools

3. Digest Library - https://ruby-doc.org/stdlib-2.6.3/libdoc/digest/rdoc/Digest.html

That's it! We hope this tutorial helped you understand different ways of generating UUID in Ruby. Happy coding!