From: Jonan Scheffler Date: 2012-03-25T22:01:42+09:00 Subject: Re: Array handling trouble for new guy --f46d043bde66c4b05904bc10dbf6 Content-Type: text/plain; charset=ISO-8859-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 Scheffler 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" 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/. >> >> -- Jonan Scheffler jonanscheffler@gmail.com (503) 985-6626 --f46d043bde66c4b05904bc10dbf6 Content-Type: text/html; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable OK so I just looked at this again and decided my answer isn't very help= ful.=A0

It looks like you're reassigning empty array= s every time your first/middle/last_names methods are called.
Try this:

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

=A0def= initialize ()
=A0 =A0@first_names =3D []
=A0 =A0@middl= e_names =3D []
=A0 =A0@last_names =3D []

=A0 =A0name_file = =3D File.open('c:\ruby\data.txt')

=A0 =A0n= ame_file.each_line do |name|
=A0 =A0 =A0@first_names.push(name.to= _s) if name =3D~ /1/
=A0 =A0 =A0@middle_names.push(name.to_s) if name =3D~ /2/
= =A0 =A0 =A0@last_names.push(name.to_s) if name =3D~ /3/
=A0 =A0en= d

=A0 =A0name_file.close
=A0end

=A0def print_names ()
=A0 =A0print "First Names"
=A0 =A0print @first_nam= es

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

=A0 =A0print "Las= t Names"
=A0 =A0print @last_names
=A0end
end

=
working =3D Namer.new
working.print_names
<= div>
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 =3D~ /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. =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/.




--
Jonan S= cheffler
(503) 985-6626

--f46d043bde66c4b05904bc10dbf6--