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
--
Posted via http://www.ruby-forum.com/.