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”]

Mario R. 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 helps
Cheers

Hi –

On Tue, 6 Nov 2007, Mario R. wrote:

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

It works…
Thank you so much

On 11/6/07, Mario R. [email protected] 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.

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 :slight_smile:

Cheers,
Peter


http://www.rubyrailways.com
http://scrubyt.org

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

Mario R. 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

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?

Hi,

Mario R. wrote:

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