Name generator | I need feedback

J

Justin D.

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
 
A

Anurag Priyam

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]
 
J

Justin D.

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"?
 
A

Anurag Priyam

Does File.open opens in "r" or "w"?

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

$ ri File.open

(from ruby core)
------------------------------------------------------------------------------
File.open(filename, mode="r" [, opt]) -> file
File.open(filename [, mode [, perm]] [, opt]) -> file
File.open(filename, mode="r" [, opt]) {|file| block } -> obj
File.open(filename [, mode [, perm]] [, opt]) {|file| block } -> obj

Hence, 'r'.
 
R

Rob Biedenharn

wrote: [...]
Does File.open opens in "r" or "w"?

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

$ ri File.open

(from ruby core)
------------------------------------------------------------------------------
File.open(filename, mode="r" [, opt]) -> file
File.open(filename [, mode [, perm]] [, opt]) -> file
File.open(filename, mode="r" [, opt]) {|file| block } -> obj
File.open(filename [, mode [, perm]] [, opt]) {|file| block } -> obj

Hence, 'r'.


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

-Rob

Rob Biedenharn
(e-mail address removed) http://AgileConsultingLLC.com/
(e-mail address removed) http://GaslightSoftware.com/
 
D

danfan46

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
 
J

Justin D.

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

Thanks again!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top