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 > Passing all parameters to a method with a unique variable
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
Passing all parameters to a method with a unique variable

Réponse
 
LinkBack Outils de la discussion
Vieux 06/11/2007, 10h46   #1
Mario Ruiz
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Passing all parameters to a method with a unique variable

I'm trying to pass to a method all the parameters in a global variable:

$allparameters="dos","ie","4","alone","","8"

...

myclass.mymethod($allparameters)


I'm doing that because I'm using this parameters a lot.
The error is: wrong number of arguments (1 for 6) (ArgumentError)

I tried also: $allparameters=["dos","ie","4","alone","","8"]
--
Posted via http://www.ruby-forum.com/.

  Réponse avec citation
Vieux 06/11/2007, 10h50   #2
David A. Black
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Passing all parameters to a method with a unique variable

Hi --

On Tue, 6 Nov 2007, Mario Ruiz wrote:

> I'm trying to pass to a method all the parameters in a global variable:
>
> $allparameters="dos","ie","4","alone","","8"
>
> ...
>
> myclass.mymethod($allparameters)
>
>
> I'm doing that because I'm using this parameters a lot.
> The error is: wrong number of arguments (1 for 6) (ArgumentError)
>
> I tried also: $allparameters=["dos","ie","4","alone","","8"]


You need to "unarray" the array, with the unary *:

myclass.mymethod(*$allparameters)


David

--
Upcoming training by David A. Black/Ruby Power and Light, LLC:
* Advancing With Rails, Edison, NJ, November 6-9
* Advancing With Rails, Berlin, Germany, November 19-22
* Intro to Rails, London, UK, December 3-6 (by Skills Matter)
See http://www.rubypal.com for details!

  Réponse avec citation
Vieux 06/11/2007, 10h51   #3
Shuaib Zahda
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Passing all parameters to a method with a unique variabl

Mario Ruiz wrote:
> I'm trying to pass to a method all the parameters in a global variable:
>
> $allparameters="dos","ie","4","alone","","8"
>
> ...
>
> myclass.mymethod($allparameters)
>
>
> I'm doing that because I'm using this parameters a lot.
> The error is: wrong number of arguments (1 for 6) (ArgumentError)
>
> I tried also: $allparameters=["dos","ie","4","alone","","8"]


I believe that in your method definition you have passed 6 parameters

def mymethod(v1, v2, v3, v4, v5, v6)
...
end

you need to only pass one parameter
def mymethod(v1)
...
end

and inside your method you have to extract the values to suit your
needs.

I wish this s
Cheers
--
Posted via http://www.ruby-forum.com/.

  Réponse avec citation
Vieux 06/11/2007, 10h51   #4
Jano Svitok
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Passing all parameters to a method with a unique variable

On 11/6/07, Mario Ruiz <mario@betware.com> wrote:
> I'm trying to pass to a method all the parameters in a global variable:
>
> $allparameters="dos","ie","4","alone","","8"
>
> ...
>

- myclass.mymethod($allparameters)
+ myclass.mymethod(*$allparameters)
>
>
> I'm doing that because I'm using this parameters a lot.
> The error is: wrong number of arguments (1 for 6) (ArgumentError)
>
> I tried also: $allparameters=["dos","ie","4","alone","","8"]


The "splash" operator * sort of "expands" the contents of the array.

Note that it's better to avoid global variables if possible.

J.

  Réponse avec citation
Vieux 06/11/2007, 10h55   #5
Mario Ruiz
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Passing all parameters to a method with a unique variable

It works...
Thank you so much
--
Posted via http://www.ruby-forum.com/.

  Réponse avec citation
Vieux 06/11/2007, 11h03   #6
Peter Szinek
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Passing all parameters to a method with a unique variable

Hi Mario,

> I'm trying to pass to a method all the parameters in a global variable:


To answer your question:

$allparameters=["dos","ie","4","alone","","8"]

and

$allparameters="dos","ie","4","alone","","8"

does the same - it assigns the array ["dos","ie","4","alone","","8"] to
the global variable $allparameters.

After this, you want

myclass.mymethod(*$allparameters)

The problem is that myclass.mymethod expects 6 parameters, whereas you
pass in an array containing 6 things (but that's still 1 parameter, no
matter how much of what it contains). The splat or unnarray operator (*)
splits it up into 6 pieces which is what your function wants.

I am quite sure that more people will join this thread, so I am leaving
the "never use global variables", "using 6 unnamed params is probably
not a very good idea", "allparameters is not the Ruby way to name a
variable" style comments to them :-)

Cheers,
Peter
___
http://www.rubyrailways.com
http://scrubyt.org


  Réponse avec citation
Vieux 06/11/2007, 11h16   #7
Mario Ruiz
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Passing all parameters to a method with a unique variable

Thanks for your comments.
This is only an example the real name of my global variable is:
$txLoginData
--
Posted via http://www.ruby-forum.com/.

  Réponse avec citation
Vieux 06/11/2007, 11h32   #8
Peter Szinek
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Passing all parameters to a method with a unique variable

Mario Ruiz wrote:
> Thanks for your comments.
> This is only an example the real name of my global variable is:
> $txLoginData


That's only a bit better :-). The Ruby convention would be to call it
tx_login_data in this case. However, this is really up to you as far as
you are consistent in naming your variables (or you'd like to merge your
code later with somebody using different naming conventions, e.g. Ruby's).

What's worse is the usage of global variables which is not recommended.
I am not sure what are you working on though - maybe you are porting
something over from a different language which is full of global
variables, and you are not going to use this code too much in the future.

However, if that's not the case, and mainly if you are writing the code
from scratch, factoring out the global variables would be a good idea in
99.9% of the cases...

2c,
Peter
___
http://www.rubyrailways.com
http://scrubyt.org



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


É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,14616 seconds with 16 queries