I'm dipping my toes into Ruby and have hit a stumbling block. I'm
attempting to write a random name generator as an exercise for myself.
So far I've got it reading from a data file but can't seem to get it to
populate the arrays. I'm sure I'm missing something as far as variable
scope goes but can't seem to dig it out of the documentation. Each name
in the file has a number at the end to identify whether or not it's
first,
middle or last. I figure I'll .chop the final array to get rid of the
extra character but haven't gotten that far. Any help would be greatly
appreciated.
class Namer # Intending it to be a random name generator eventually
def first_names
@first_names=[]
end
def middle_names
@middle_names=[]
end
def last_names
@last_names=[]
end
def initialize ()
name_file = File.open('c:\ruby\data.txt')
name_file.each_line do |name|
# just a test to see if I was pulling in the data correctly, seems to
work
print name.to_s
# This is the section that I can't figure out. Nothing gets pushed
into the arrays
first_names.push(name.to_s) if name =~ /1/
middle_names.push(name.to_s) if name =~ /2/
last_names.push(name.to_s) if name =~ /3/
Your instance variables aren't being used consistently.
Try taking your first_names method apart and pushing directly on to the
instance variable.
@first_names.push(name) if name =~ /1/
···
On Mar 25, 2012 8:14 AM, "Aaron Brink" <lists@ruby-forum.com> wrote:
Greetings all,
I'm dipping my toes into Ruby and have hit a stumbling block. I'm
attempting to write a random name generator as an exercise for myself.
So far I've got it reading from a data file but can't seem to get it to
populate the arrays. I'm sure I'm missing something as far as variable
scope goes but can't seem to dig it out of the documentation. Each name
in the file has a number at the end to identify whether or not it's
first,
middle or last. I figure I'll .chop the final array to get rid of the
extra character but haven't gotten that far. Any help would be greatly
appreciated.
class Namer # Intending it to be a random name generator eventually
def first_names
@first_names=
end
def middle_names
@middle_names=
end
def last_names
@last_names=
end
def initialize ()
name_file = File.open('c:\ruby\data.txt')
name_file.each_line do |name|
# just a test to see if I was pulling in the data correctly, seems to
work
print name.to_s
# This is the section that I can't figure out. Nothing gets pushed
into the arrays
first_names.push(name.to_s) if name =~ /1/
middle_names.push(name.to_s) if name =~ /2/
last_names.push(name.to_s) if name =~ /3/
Your methods first_names, middle_names etc. don't make sense. Every time
you call them, the corresponding instance variable will be overwritten
with an empty array. The current content gets lost. This may even happen
after you created the object, since all three methods are public.
I don't even understand what they're meant for. Why not simply
initialize the variables @first_names, @second_names etc. with empty
arrays and fill them directly by @first_names.push ... ?
There are some other issues in your code:
- You shouldn't put space between a method name and the parenthesized
arguments. While this does seem to work in method definitions, it will
fail when calling the method. For example, you cannot write
print (1, 2)
- Don't use single backslashes in strings. They're meta characters for
escaping, so you cannot rely on them being put into the string
literally. Instead, use double backslashes or slashes (this works in
Windows, too: 'c:/ruby/data.txt')
- When you read from a file, you don't have to convert the input into
strings. The input is a string already.
Thanks a lot for the input, it's definitely helped to pull the splinter
out of my brain. I appreciate the rapid response to such a trivial
question.
Most of my trouble stems from just not knowing the language well enough.
I've cobbled together bits of code from various examples without a
complete understanding of what they were doing. Pulling the variables
out of the methods makes perfect sense but whatever example I was
looking at was defining variables that way within the class so I ran
with it.
name_file.each_line do |name| @first_names.push(name.to_s) if name =~ /1/ @middle_names.push(name.to_s) if name =~ /2/ @last_names.push(name.to_s) if name =~ /3/
end
On Sun, Mar 25, 2012 at 8:29 AM, Jonan Scheffler <jonanscheffler@gmail.com>wrote:
Your instance variables aren't being used consistently.
Try taking your first_names method apart and pushing directly on to the
instance variable.
@first_names.push(name) if name =~ /1/
On Mar 25, 2012 8:14 AM, "Aaron Brink" <lists@ruby-forum.com> wrote:
Greetings all,
I'm dipping my toes into Ruby and have hit a stumbling block. I'm
attempting to write a random name generator as an exercise for myself.
So far I've got it reading from a data file but can't seem to get it to
populate the arrays. I'm sure I'm missing something as far as variable
scope goes but can't seem to dig it out of the documentation. Each name
in the file has a number at the end to identify whether or not it's
first,
middle or last. I figure I'll .chop the final array to get rid of the
extra character but haven't gotten that far. Any help would be greatly
appreciated.
class Namer # Intending it to be a random name generator eventually
def first_names
@first_names=
end
def middle_names
@middle_names=
end
def last_names
@last_names=
end
def initialize ()
name_file = File.open('c:\ruby\data.txt')
name_file.each_line do |name|
# just a test to see if I was pulling in the data correctly, seems to
work
print name.to_s
# This is the section that I can't figure out. Nothing gets pushed
into the arrays
first_names.push(name.to_s) if name =~ /1/
middle_names.push(name.to_s) if name =~ /2/
last_names.push(name.to_s) if name =~ /3/