Array handling trouble for new guy

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/

end
name_file.close

end

def print_names ()
print “First Names”
print first_names

print "Middle Names"
print middle_names

print "Last Names"
print last_names

end
end

working = Namer.new
working.print_names

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/

OK so I just looked at this again and decided my answer isn’t very
helpful.

It looks like you’re reassigning empty arrays every time your
first/middle/last_names methods are called.

Try this:

class Namer # Intending it to be a random name generator eventually

def initialize ()
@first_names = []
@middle_names = []
@last_names = []

name_file = File.open(‘c:\ruby\data.txt’)

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

name_file.close
end

def print_names ()
print “First Names”
print @first_names

print “Middle Names”
print @middle_names

print “Last Names”
print @last_names
end
end

working = Namer.new
working.print_names

On Sun, Mar 25, 2012 at 8:29 AM, Jonan S.

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.

Thanks again, and happy coding.

Hi,

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.

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