Passing all parameters to a method with a unique variable

M

Mario Ruiz

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"]
 
D

David A. Black

Hi --

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

Shuaib Zahda

Mario said:
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 helps
Cheers
 
J

Jano Svitok

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

Peter Szinek

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
 
M

Mario Ruiz

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

Peter Szinek

Mario said:
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
 
M

Mario Ruiz

I'm using global variables because I have a settings file with all the
global variables and the final user can change the values only in that
file.
I don't have any practice in Ruby (2 weeks), is there a better way?
 
P

Peter Szinek

Hi,

Mario said:
I'm using global variables because I have a settings file with all the
global variables and the final user can change the values only in that
file.
As far as I understand the situation (maybe I don't), this has nothing
to do with the scope of the variables used.
The notion of a 'settings file', as used in other languages like Java,
is quite alien to Ruby. In Ruby, the configurations stuff is code, too
(a typical example of this is config/environment.rb in Rails).
I don't have any practice in Ruby (2 weeks), is there a better way?
Yeah for sure. We would need a more detailed description of the problem
to come up with a solution, though.

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

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
474,270
Messages
2,571,348
Members
48,034
Latest member
JaimieBarn

Latest Threads

Top