PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Autres forums > Forum Programmation & Conception > comp.lang.ruby > using portions of other methods in a new method
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
using portions of other methods in a new method

Réponse
 
LinkBack Outils de la discussion
Vieux 07/06/2008, 14h10   #1
Jason Lillywhite
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut using portions of other methods in a new method

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

  Réponse avec citation
Vieux 07/06/2008, 14h32   #2
Eric I.
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: using portions of other methods in a new method

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.
  Réponse avec citation
Vieux 07/06/2008, 14h53   #3
Jason Lillywhite
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: using portions of other methods in a new method

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

  Réponse avec citation
Vieux 07/06/2008, 14h57   #4
Sebastian Hungerecker
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: using portions of other methods in a new method

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

  Réponse avec citation
Vieux 07/06/2008, 15h15   #5
Heesob Park
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: using portions of other methods in a new method

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

  Réponse avec citation
Vieux 07/06/2008, 15h17   #6
Jason Lillywhite
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: using portions of other methods in a new method

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

  Réponse avec citation
Vieux 07/06/2008, 15h22   #7
Jason Lillywhite
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: using portions of other methods in a new method

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

  Réponse avec citation
Vieux 07/06/2008, 15h30   #8
Sebastian Hungerecker
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: using portions of other methods in a new method

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

  Réponse avec citation
Réponse


Outils de la discussion

Règles de messages
Vous ne pouvez pas créer de nouvelles discussions
Vous ne pouvez pas envoyer des réponses
Vous ne pouvez pas envoyer des pièces jointes
Vous ne pouvez pas modifier vos messages

Les balises BB sont activées : oui
Les smileys sont activés : oui
La balise [IMG] est activée : oui
Le code HTML peut être employé : non
Trackbacks are oui
Pingbacks are oui
Refbacks are oui


Fuseau horaire GMT +1. Il est actuellement 02h16.


Édité par : vBulletin® version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC5 Tous droits réservés.
Version française #16 par l'association vBulletin francophone
PHWinfo est un site Éducation Sans Frontières
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,16124 seconds with 16 queries