Array sample method?
Is your feature request related to a problem? Please describe.
Ruby 1.9+ will let you do something like ['foo', 'bar', 'baz'].sample # => 'bar' natively, but this won't respect Faker's own RNG seed, meaning the results will be nondeterministic across separate executions.
(This has been brought up before in #1113.)
Describe alternatives you've considered
A deterministic Array#sample can be approximated with a custom method:
def sample(*elements)
elements[Faker::number.between(from: 0, to: elements.count - 1)]
end
But this has to live somewhere in my code and has to be called differently than everything else in the constructed fake thing. Is this a huge deal? No definitely not, but it's not as simple or clear as it could be.
Additional context
It would be lovely to have a shortcut method such as Faker::Array.sample([...]) that made this a deterministic one-liner and maintained the DSL pattern to which folks are accustomed.
If it helps make the case, other Faker implementations already offer varieties of this:
- PHP:
randomElement($array = array ('a','b','c')) // 'b' - JS:
faker.random.arrayElement() - Python:
fake.random_element(elements=('a', 'b', 'c', 'd'))
Thanks for your time and effort!
Worth mentioning: I'd be happy to put together a PR if there's some indication it'd be accepted.
I respect the time everyone volunteers to maintain a FOSS project like this but I also ask for my own time to be respected, so that indication of which way the maintainers lean is what I need to move from writing up an issue to submitting a PR. Thanks! 🙇
Hey, folks. In an effort to lighten our load as maintainers and be able to serve you better in the future, the faker-ruby team is working on cleaning out the cobwebs in this repo by pruning the backlog. As there are few of us, there are a lot of items that will simply never earn our attention in a reasonable time frame, and rather than giving you an empty promise, we think it makes more sense to focus on more recent issues. That means, unfortunately, that we must close this issue.
Don't take this the wrong way: our aim is not to diminish the effort people have made or dismiss problems that have been raised. If you feel that we should reopen this issue, then please let us know so that we can reprioritize it. Thanks!
For anyone in the future trying to find this, you probably want Faker::Base.sample(your_array)