From: Jonan Scheffler Date: 2012-03-25T21:29:24+09:00 Subject: Re: Array handling trouble for new guy --f46d044303f037ad0f04bc106851 Content-Type: text/plain; charset=ISO-8859-1 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" 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/ > > 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/. > > --f46d044303f037ad0f04bc106851 Content-Type: text/html; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable

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 =3D~ /1/

On Mar 25, 2012 8:14 AM, "Aaron Brink"= <lists@ruby-forum.com> w= rote:
Greetings all,

I'm dipping my toes into Ruby and have hit a stumbling block. =A0I'= 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 i= t to
populate the arrays. =A0I'm sure I'm missing something as far as va= riable
scope goes but can't seem to dig it out of the documentation. =A0Each n= ame
in the file has a number at the end to identify whether or not it's
first,
middle or last. =A0I figure I'll .chop the final array to get rid of th= e
extra character but haven't gotten that far. =A0Any help would be great= ly
appreciated.



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

=A0def first_names
=A0 =A0@first_names=3D[]
=A0end

=A0def middle_names
=A0 =A0@middle_names=3D[]
=A0end

=A0def last_names
=A0 =A0@last_names=3D[]
=A0end

=A0def initialize ()
=A0 =A0name_file =3D File.open('c:\ruby\data.txt')
=A0 =A0name_file.each_line do |name|

# just a test to see if I was pulling in the data correctly, seems to
work
=A0 =A0 print name.to_s


# =A0This is the section that I can't figure out. =A0Nothing gets pushe= d
into the arrays
=A0 =A0 first_names.push(name.to_s) if name =3D~ /1/
=A0 =A0 middle_names.push(name.to_s) if name =3D~ /2/
=A0 =A0 last_names.push(name.to_s) if name =3D~ /3/

=A0 =A0end
=A0 =A0name_file.close
=A0end

=A0def print_names ()
=A0 =A0print "First Names"
=A0 =A0print first_names

=A0 =A0print "Middle Names"
=A0 =A0print middle_names

=A0 =A0print "Last Names"
=A0 =A0print last_names
=A0end
end

working =3D Namer.new
working.print_names

--
Posted via http://= www.ruby-forum.com/.

--f46d044303f037ad0f04bc106851--