using portions of other methods in a new method

  • Thread starter Jason Lillywhite
  • Start date
J

Jason Lillywhite

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!
 
E

Eric I.

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://LearnRuby.com for all the details.
 
J

Jason Lillywhite

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

Sebastian Hungerecker

Jason said:
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
 
H

Heesob Park

Hi,

2008/6/7 Jason Lillywhite said:
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
 
J

Jason Lillywhite

Sebastian said:
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
 
J

Jason Lillywhite

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?
 
S

Sebastian Hungerecker

Jason said:
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
 
B

Brian Kohl

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: (e-mail address removed)
Subject: Re: using portions of other methods in a new method
To: (e-mail address removed)
=20
Sebastian said:
=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=
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top