[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

using portions of other methods in a new method

Jason

6/7/2008 1:11:00 PM

How do you take a piece of a method and use it in another? Here is my
simple example:

class MyArray
def counter
result = []
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
i += 1
end
puts result
end
def counter2
counter
end
end

test = MyArray.new
puts test.counter2

I want counter2 method to take the array "b" and do something with it,
like this:
b.inject {|sum, element| sum+element}
I just can't figure out how to transfer b from one method to another. Is
there a clean way to do this?

Thank you!
--
Posted via http://www.ruby-....

9 Answers

Eric I.

6/7/2008 1:32:00 PM

0

On Jun 7, 9:10 am, Jason Lillywhite <jason.lillywh...@gmail.com>
wrote:
> How do you take a piece of a method and use it in another? Here is my
> simple example:
>
> class MyArray
>   def counter
>     result = []
>     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
>       i += 1
>     end
>     puts result
>   end
>   def counter2
>     counter
>   end
> end
>
> test = MyArray.new
> puts test.counter2
>
> I want counter2 method to take the array "b" and do something with it,
> like this:
> b.inject {|sum, element| sum+element}
> I just can't figure out how to transfer b from one method to another. Is
> there a clean way to do this?

Hi Jason,

If I understand you correctly, you want your counter method to return
b to its caller. The method will return the value of the last
expression evaluated, or alternatively you can use a return statement
anywhere within the method.

====

class MyArray
def counter
result = []
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
i += 1
end
puts result
b # will return b to caller
end
def counter2
this_b = counter # capture the return value
this_b.inject {|sum, element| sum+element} # total is returned to
caller
end
end
test = MyArray.new
puts test.counter2

====

Hope that helps,

Eric

====

LearnRuby.com offers Rails & Ruby HANDS-ON public & ON-SITE
workshops.
Ruby Fundamentals Wkshp June 16-18 Ann Arbor, Mich.
Ready for Rails Ruby Wkshp June 23-24 Ann Arbor, Mich.
Ruby on Rails Wkshp June 25-27 Ann Arbor, Mich.
Ruby Plus Rails Combo Wkshp June 23-27 Ann Arbor, Mich
Please visit http://Lea... for all the details.

Jason

6/7/2008 1:54:00 PM

0

Thanks Eric!
however, it appears when you capture the return value b by using the
name of the method it comes from, you only get the last value of the
array and not the array itself.

the method 'counter' returns the array:
1.5 2
3.0 3
4.5 4
6.0 5
7.5 6
9.0 7

and I want to take array 'b' which is:
1.5
3.0
4.5
6.0
7.5
9.0

and do stuff with it, maybe even just print it out alone by calling the
second method 'counter2'. My goal here is to learn how to take only one
of the arrays from a method and use it in another.
--
Posted via http://www.ruby-....

Sebastian Hungerecker

6/7/2008 1:58:00 PM

0

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
--
Jabber: sepp2k@jabber.org
ICQ: 205544826

Heesob Park

6/7/2008 2:15:00 PM

0

Hi,

2008/6/7 Jason Lillywhite <jason.lillywhite@gmail.com>:
> Thanks Eric!
> however, it appears when you capture the return value b by using the
> name of the method it comes from, you only get the last value of the
> array and not the array itself.
>
> the method 'counter' returns the array:
> 1.5 2
> 3.0 3
> 4.5 4
> 6.0 5
> 7.5 6
> 9.0 7
>
> and I want to take array 'b' which is:
> 1.5
> 3.0
> 4.5
> 6.0
> 7.5
> 9.0
>
> and do stuff with it, maybe even just print it out alone by calling the
> second method 'counter2'. My goal here is to learn how to take only one
> of the arrays from a method and use it in another.

Do you want something like this?

def counter
return ["1.5\t2","3.0\t3","4.5\t4","6.0\t5","7.5\t6","9.0\t7"]
end

def counter2
b = counter.map{|x|x.split(/\t/).first}
puts b
end


Regards,

Park Heesob

Jason

6/7/2008 2:17:00 PM

0

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-....

Jason

6/7/2008 2:22:00 PM

0

I'm sorry, I meant What if I wanted to create an array_a and array_b and
refer to both of those in another method. Does that make sense?
--
Posted via http://www.ruby-....

Sebastian Hungerecker

6/7/2008 2:30:00 PM

0

Jason Lillywhite wrote:
> I'm sorry, I meant What if I wanted to create an array_a and array_b and
> refer to both of those in another method. Does that make sense?

Either return an array, containing both array_a and array_b as subarrays, or
store one of them in an instance variable.

Examples:
# Option a:
class MyClass
def mymethod
...dostuff...
[arraya, arrayb]
end
def myothermethod
aa, ab = mymethod
...dostuff...
end
end

# Option b:
class MyClass
def mymethod
...dostuff...
@b = arrayb
arraya
end
def myothermethod
aa = mymethod
ab = @b
...dostuff...
end
end

HTH,
Sebastian
--
NP: In Flames - Worlds Within The Margin
Jabber: sepp2k@jabber.org
ICQ: 205544826

Jason

6/7/2008 2:38:00 PM

0

Thank you! Instance variables! I should have realized that.
--
Posted via http://www.ruby-....

Brian Kohl

6/7/2008 2:50:00 PM

0

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-....
>=20

_________________________________________________________________
Search that pays you back! Introducing Live Search cashback.
http://search.live.com/cashback/?&pkw=3Dform=3DMIJAAF/publ=3DHMTGL...
rchpaysyouback=