Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Faker::Internet.uuid #1603

Merged
merged 2 commits into from May 10, 2019
Merged

Add Faker::Internet.uuid #1603

merged 2 commits into from May 10, 2019

Conversation

ianks
Copy link
Contributor

@ianks ianks commented May 2, 2019

UUIDs are incredibly common. Many databases, APIs, etc will use them to identify resources. Being able to generate these easily is paramount to having a productive development environment.

So, why not just use SecureRandom.uuid?

The beauty of faker is that is can be used to generate deterministic sequences of numbers in a non-deterministic world. This makes debugging much easier, as we have one less thing to reason about.

This patch uses fakers PRNG to build a UUID, which solves this problem.

supersedes #943

@@ -192,6 +192,10 @@ def user_agent(vendor = nil)
sample(agents)
end

def uuid
Faker::Config.random.bytes(16).unpack('H8H4H4H4H12').join('-')
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

probably best to generate a valid version4 UUID incase there's a library out there that does something special based on the version. For Faker's purposes, that just means that we need to make sure the first digit in the 3rd block is always a 4:
xxxxxxxx-xxxx-4xxx-xxxx-xxxxxxxxxxxx

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great catch. Done.

Copy link
Contributor

@Zeragamba Zeragamba left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

looks good to me

@ianks
Copy link
Contributor Author

ianks commented May 2, 2019

🎊

Copy link
Member

@vbrazo vbrazo left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

agreed 👍

@vbrazo vbrazo merged commit 289c719 into faker-ruby:master May 10, 2019
@ianks ianks deleted the uuid branch May 10, 2019 04:15
michebble pushed a commit to michebble/faker that referenced this pull request Feb 16, 2020
* Add Faker::Internet.uuid

* Make uuid v4 compatible
davidmorton0 pushed a commit to davidmorton0/faker that referenced this pull request Jul 12, 2021
* Add Faker::Internet.uuid

* Make uuid v4 compatible
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants