I've finished up a working version of my random name generator. Here's a gist of the code:
Random Name Generator
I'm calling it aan (short for adjective, adjective, noun) because the original was just three phrases slapped together. Now I've added functionality that will generate a prefix or a suffix (seemingly) at random. I might spend a bit more time working with this. It's been a fun little project and I'd like to make a Rails project for it so I can put it up on Heroku and show it to non-coders.
Some other things I may look into:
A way to output the full name on a single line. I tried print instead of puts, but it simply outputs the pieces of the array, not the entire string built from the various arrays.
A way to capitalize every word except for words like "of", "the" and so on.
Using this with other data. What about a Full House episode plot generator? What if instead of specifically saying "Uncle Jesse takes Stephanie to the movies, which makes DJ incredibly jealous" I had "DAD takes DAUGHTER1 to the EVENT, which makes DAUGHTER2 incredibly jealous" and I passed in variables, to further randomize output? Maybe this will be my next project. It kind of combines stuff I was doing with the MadLib generator and this random name generator.
One of the things I like most about Ruby is how much you can do with it once you have an idea of what you're doing. I've definitely got a better idea of how reading files and using data found in those files works now. It's also pretty awesome to sit down for a couple of hours and bang out a working program.
I've also found that since I've become less focused on making a career change and more interested in learning how Ruby works, I'm having a lot more fun coding. That's a plus, right?
A few thoughts:
ReplyDelete- You might use YAML for storing your word lists. Read them in as a hash of arrays so that you can just say words[adj1].
- You can use Array#sample to get a random element from an array.
- Use string interpolation to construct your phrase, e.g. "#{words[adj1].sample} #{words[adj2].sample} #{words[noun].sample}", or use Array#join to join the strings together.
- Check out the source for the methods in ActiveSupport::Inflector to see how Rails humanize, titleize, etc. work.
Also, you're initializing new_phrase as an array, then using << to add the phrase parts array to that (so you've got a nested array). You can just assign the chosen array to a variable when it makes sense. Also, case returns a value, so you could rewrite as:
ReplyDeletenew_phrase = case rand(7)
when...
[adj1, adj2, noun]
...
Just to get an idea of some of the things you could try out:
ReplyDeletehttps://gist.github.com/1354158
@Matt awesome, I'm looking through the gist you posted and slowly digesting it. That yaml trick is siiiiiiiiick, doggie!
ReplyDeleteAlso, I was trying to figure out a way to make the code repeat until the user stopped it. I had a while loop that just spit out the same phrase, but didn't think of doing a loop / break.