|
|
|
|
||||||
![]() |
|
|
LinkBack | Outils de la discussion |
|
|
#1 |
|
Messages: n/a
Hébergeur: |
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-forum.com/. |
|
|
|
#2 |
|
Messages: n/a
Hébergeur: |
On Jun 7, 9:10am, 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 s, 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. |
|
|
|
#3 |
|
Messages: n/a
Hébergeur: |
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-forum.com/. |
|
|
|
#4 |
|
Messages: n/a
Hébergeur: |
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 |
|
|
|
#5 |
|
Messages: n/a
Hébergeur: |
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 |
|
|
|
#6 |
|
Messages: n/a
Hébergeur: |
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/. |
|
|
|
#7 |
|
Messages: n/a
Hébergeur: |
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-forum.com/. |
|
|
|
#8 |
|
Messages: n/a
Hébergeur: |
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 |
|
![]() |
| Outils de la discussion | |
|
|