|
|
|
|
||||||
![]() |
|
|
LinkBack | Outils de la discussion |
|
|
#1 |
|
Messages: n/a
Hébergeur: |
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/. |
|
|
|
#2 |
|
Messages: n/a
Hébergeur: |
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! |
|
|
|
#3 |
|
Messages: n/a
Hébergeur: |
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/. |
|
|
|
#4 |
|
Messages: n/a
Hébergeur: |
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. |
|
|
|
#5 |
|
Messages: n/a
Hébergeur: |
|
|
|
|
#6 |
|
Messages: n/a
Hébergeur: |
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 |
|
|
|
#7 |
|
Messages: n/a
Hébergeur: |
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/. |
|
|
|
#8 |
|
Messages: n/a
Hébergeur: |
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 |
|
![]() |
| Outils de la discussion | |
|
|