diff --git a/spec/std/uuid_spec.cr b/spec/std/uuid_spec.cr index 5a4d46721595..7a5167f8f885 100644 --- a/spec/std/uuid_spec.cr +++ b/spec/std/uuid_spec.cr @@ -10,6 +10,16 @@ describe "UUID" do end end + describe "#<=>" do + it "correctly compares two UUIDs" do + uuid_1 = UUID.new("00330000-0000-0000-0000-000000000000") + uuid_2 = UUID.new("00000011-0000-0000-5500-000099000000") + (uuid_1 <=> uuid_2).should be > 0 + (uuid_2 <=> uuid_1).should be < 0 + (uuid_1 <=> uuid_1).should eq 0 + end + end + describe "random initialize" do it "works with no options" do subject = UUID.random diff --git a/src/uuid.cr b/src/uuid.cr index e4f86117136f..994813477a03 100644 --- a/src/uuid.cr +++ b/src/uuid.cr @@ -1,5 +1,7 @@ # Represents a UUID (Universally Unique IDentifier). struct UUID + include Comparable(UUID) + # Variants with 16 bytes. enum Variant # Unknown (i.e. custom, your own). @@ -207,6 +209,10 @@ struct UUID end end + def <=>(other : UUID) : Int32 + @bytes <=> other.bytes + end + class Error < Exception end