--_884f8836-990a-4bbd-9a63-e30c89f690e6_
Content-Type: text/plain; charset="iso-8859-1"
Content-Transfer-Encoding: quoted-printable
Jason,
If you want both result_a and result_b to be returned from your counter method you can easily do that as Ruby allows for any number of elements to be returned from a method.
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 = counter
puts this_b.inject {|sum, element| sum+element} # total is returned
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
>
> Sebastian Hungerecker wrote:
> > Jason Lillywhite wrote:
> >> I want counter2 method to take the array "b" and do something with it
> >
> > There is no array b in your code. b is a number - I think you want the
> > array
> > result.
> >
> > HTH,
> > Sebastian
>
> Oh, you are right. Okay now I see what I need thanks to both of you. I
> need to set up an array that captures just b alone as shown in my new
> code below. My lesson from this is call stuff from other methods by
> calling the name of the method and it will get you the last statement of
> the method. BUT WHAT IF I WANT TO GET MULTIPLE ITEMS FROM THE 'counter'
> METHOD? This way only lets you get the last line from the method, but
> what if I wanted to grab a "result_b"?
>
> class MyArray
> def counter
> result = []
> result_b = []
> i = 0
> a = 1
> b = 0
> while i <= 5 do
> a = a + 1
> b = b +1.5
> result << b.to_s + "\t" + a.to_s
> result_b << b
> i += 1
> end
> result
> result_b # will return result_b to caller
> end
> def counter2
> this_b = counter # capture the return value
> puts this_b.inject {|sum, element| sum+element} # total is returned
> to
> caller
> end
> end
> test = MyArray.new
> puts test.counter2
>
> --
> Posted via http://www.ruby-forum.com/.
>
_________________________________________________________________
Search that pays you back! Introducing Live Search cashback.
http://search.live.com/cashback/?&pkw=form=MIJAAF/publ=HMTGL/crea=srchpaysyouback-_884f8836-990a-4bbd-9a63-e30c89f690e6_--