Name generator | I need feedback

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!

EDIT SOLVED

EDIT_2
I put a carriage return after the – => my program was interpreting like
this : “”

END_EDIT_2

Using my generator, I was able to generate these names :

kermit tiger
lilac cat
yellow fox
kermit hippo
navy gorilla
violethorse
apple pinguin
citruscat
mintzebra
tangerinehorse
kermitpanda
forest fox
graphitetiger
red ball
black owl
gold dog
kermitball
kermit owl
cocoafox
tangerinehippo
aqua zebra
winehippo
cofeehippo
navy snake
lilac rabbit
yellow dog
sunshineracoon
tomato hippo
cofeewolf
orchidowl
sanddog
blue whale
tealpinguin
redcow
tangerine
tofeelion
graphitepanda
blackdog
bubblegumcat
red ball
orangerabbit
limeturtle
yellowgorilla
redfrog
brick
tofee beef
goldowl
blackhorse
violethippo
violetracoon

I’m wondering why does “violet” come twice (at the end) and why does
“brick” doesn’t have any name after…

filename = ARGV[0] || “default.txt”
file = File.new(filename, “r”)
lines = file.readlines
file.close

A better alternative since the block invocation automatically takes
care of closing the file:

lines = File.open(filename) {|f| f.readlines}

or just,

lines = File.readlines(filename)

lines = lines.to_s.split("–") # separate adjectives from nouns
adjs = lines[0].split("\n")
seps = lines[1].split("\n") # name separators (" " - _ |)
nouns = lines[2].split("\n")

Converting to string and then splitting again looks expensive.

Since your separator is fixed, why not just do:

separator = lines.index(’–\n’)
adjs = lines[0…sep]
nouns = lines[(sep+1)…-1]

Thanks a lot anurag! Your answer is exactly what I wanted =)

A better alternative since the block invocation automatically takes
care of closing the file:

lines = File.open(filename) {|f| f.readlines}

or just,

lines = File.readlines(filename)

Does File.open opens in “r” or “w”?

On Mon, Mar 21, 2011 at 2:25 AM, Justin D. [email protected]
wrote:
[…]

Does File.open opens in “r” or “w”?

It is easier to refer docs for such questions :).

$ ri File.open

(from ruby core)

On 2011-03-20 15:20, Justin D. wrote:

adjective_3
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

Thanks a lot! Your second methods looks quite effective. I’ll study your
code and try to improve the one I did.

Thanks again!

On Mar 20, 2011, at 5:10 PM, Anurag P. wrote:


…But if you need ‘w’, you can specify the mode yourself rather than
taking the default value.

-Rob

Rob B.
[email protected] http://AgileConsultingLLC.com/
[email protected] http://GaslightSoftware.com/

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs