From: Brian Kohl Date: 2008-06-07T23:50:16+09:00 Subject: Re: using portions of other methods in a new method --_884f8836-990a-4bbd-9a63-e30c89f690e6_ Content-Type: text/plain; charset="iso-8859-1" Content-Transfer-Encoding: quoted-printable Jason,=20 If you want both result_a and result_b to be returned from your counter met= hod you can easily do that as Ruby allows for any number of elements to be = returned from a method.=20 change the last line of the counter method to do the following: return result_a, result_b Then in your counter method you can have the following: def counter a,b =3D counter puts this_b.inject {|sum, element| sum+element} # total is returned=20 end Brian > Date: Sat, 7 Jun 2008 23:17:00 +0900 > From: jason.lillywhite@gmail.com > Subject: Re: using portions of other methods in a new method > To: ruby-talk@ruby-lang.org >=20 > Sebastian Hungerecker wrote: > > Jason Lillywhite wrote: > >> I want counter2 method to take the array "b" and do something with it > >=20 > > There is no array b in your code. b is a number - I think you want the= =20 > > array > > result. > >=20 > > HTH, > > Sebastian >=20 > Oh, you are right. Okay now I see what I need thanks to both of you. I=20 > need to set up an array that captures just b alone as shown in my new=20 > code below. My lesson from this is call stuff from other methods by=20 > calling the name of the method and it will get you the last statement of= =20 > the method. BUT WHAT IF I WANT TO GET MULTIPLE ITEMS FROM THE 'counter'=20 > METHOD? This way only lets you get the last line from the method, but=20 > what if I wanted to grab a "result_b"? >=20 > class MyArray > def counter > result =3D [] > result_b =3D [] > i =3D 0 > a =3D 1 > b =3D 0 > while i <=3D 5 do > a =3D a + 1 > b =3D b +1.5 > result << b.to_s + "\t" + a.to_s > result_b << b > i +=3D 1 > end > result > result_b # will return result_b to caller > end > def counter2 > this_b =3D counter # capture the return value > puts this_b.inject {|sum, element| sum+element} # total is returned= =20 > to > caller > end > end > test =3D MyArray.new > puts test.counter2 >=20 > --=20 > Posted via http://www.ruby-forum.com/. >=20 _________________________________________________________________ Search that pays you back! Introducing Live Search cashback. http://search.live.com/cashback/?&pkw=3Dform=3DMIJAAF/publ=3DHMTGL/crea=3Ds= rchpaysyouback= --_884f8836-990a-4bbd-9a63-e30c89f690e6_--