Reputation: 6805
I am using Rails 4, and I came across this cool gist the other day.
Any way, how would I do something similar with Rails where I can randomly choose from two arrays to generate a somewhat-unique name?
I know there are Gems for this (e.g., Bazaar and Faker). But I like the idea of using my own simple list.
Any help on how to get this started would be fabulous.
Upvotes: 1
Views: 555
Reputation: 24337
Here's your haiku method rewritten in Ruby. I shortened the word lists for readability.
def haiku
adjs = ["autumn", "hidden", "bitter", "misty", "silent", "empty", "dry", "dark"]
nouns = ["waterfall", "river", "breeze", "moon", "rain", "wind", "sea", "morning"]
[adjs.sample, nouns.sample].join('_')
end
puts haiku # returns random combination like "bitter_rain" or "empty_sea"
Upvotes: 5
Reputation: 7815
If you want to pick a random element from an array in ruby just use Array#sample
.
my_array = [1,2,3,4,5,6,7]
my_array.sample #gives a random element of the list
Upvotes: 2