From: danfan46 Date: 2011-03-21T16:12:21+09:00 Subject: Re: Name generator | I need feedback On 2011-03-20 15:20, Justin D. wrote: > Hello! > > I created a name generator in Ruby. Basically, I have a file > (default.txt) that contains words. These words are separated in two : > "adjectives" and "nouns". Result : adjective noun . > > In my file default.txt, I use this scheme : > > adjective_1 > adjective_2 > adjective_3 > adjective_n > -- # separator > noun_1 > noun_2 > noun_3 > noun_n > > (*NOTE*: I added another category : "separators" to separate adjectives > and nouns (eg.: " ", _ - |) => adjective_noun or adjective noun) > > I would simply like to know if my code is optimize and if it follow > Ruby's way enough. > > Thanks! > > Attachments: > http://www.ruby-forum.com/attachment/6043/name_generator.rb > http://www.ruby-forum.com/attachment/6044/default.txt > > Hi Justin! The important thing is not to make the code look smart and condensed. What is important is that it works as intended, and when you return to the program after n years, it should be easy to understand what it is doing. Works as intended? Look at the contents of seps[0] and nouns[0]. Was that your intention? The concept of reading a file into an array, convert that to a string and then split the string into three new arrays, is not very intuitive. A cleaner approach would be 1. Use three files, adjs.txt, seps.txt, nouns.txt or 2. Use a record prefix A:, S:, N: or (if you must stick to that file layout 3. iterate over the file and use a switch that changes when you match "--" to keep track of where you are in the source file. case switch when 0 then ...adjs... when 1 then ...seps... when 2 then ...nouns... end /dg