I'm a Sr. Software Engineer at Flashpoint. I specialize in Python and Go, building functional, practical, and maintainable web systems leveraging Kubernetes and the cloud. Blog opinions are my own.
Hi Tom! Thanks for sharing! How does this Gem handle collisions? One of the test cases generates all 676,000 robot names, so if calling random_example provides duplicates, we run into the same "theoretical infinite time complexity" issue, calling random_example repeatedly until it provides the one robot name we haven't encountered yet.
You could use Regexp#random_example in conjunction with the taken_names, as per your first solution in this blog post.
Or -- with the potential performance issue of storing a large array in ruby memory (as with all your other examples that use to_a!) -- you could also use Regexp#examples to end up with very similar solutions. (See the documentation.) For example:
.
...Note that this would be a bad idea if the pattern got longer, so the number of possible results was much larger. All of your examples that use to_a, just like my Regexp#examples code above, would soon freeze the system as the array length grows exponentially.
Using Regexp#random_example - similar to your original implementation - would scale fine though.
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
Hi Tom! Thanks for sharing! How does this Gem handle collisions? One of the test cases generates all 676,000 robot names, so if calling
random_exampleprovides duplicates, we run into the same "theoretical infinite time complexity" issue, callingrandom_examplerepeatedly until it provides the one robot name we haven't encountered yet.You could use
Regexp#random_examplein conjunction with thetaken_names, as per your first solution in this blog post.Or -- with the potential performance issue of storing a large array in ruby memory (as with all your other examples that use
to_a!) -- you could also useRegexp#examplesto end up with very similar solutions. (See the documentation.) For example:.
...Note that this would be a bad idea if the pattern got longer, so the number of possible results was much larger. All of your examples that use
to_a, just like myRegexp#examplescode above, would soon freeze the system as the array length grows exponentially.Using
Regexp#random_example- similar to your original implementation - would scale fine though.