1

I'm experimenting with the Crystal standard library and was wondering how to generate a UUID? The UUID.new(..) constructors all seem to expect arguments, but how do I just generate a random one?

yiwei
  • 4,022
  • 9
  • 36
  • 54

1 Answers1

2

I was looking at the wrong part of the standard library documentation -- just a bit below the constructors is the .random class method, which requires no arguments and generates a new UUID.

Usage example:

require "uuid"
puts "New UUID: #{UUID.random}" 

# Output:
# New UUID: bfc5a3cf-a138-4323-881b-764e1e798ce4
yiwei
  • 4,022
  • 9
  • 36
  • 54
  • 2
    I created a PR to fix this in the API docs. The `.random` and `.empty` methods should show up in the *Constructors* section. https://github.com/crystal-lang/crystal/pull/10539 – Johannes Müller Mar 23 '21 at 18:46