[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Passing all parameters to a method with a unique variable

Mario Ruiz

11/6/2007 10:46:00 AM

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

12 Answers

David A. Black

11/6/2007 10:51:00 AM

0

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.r... for details!

Jano Svitok

11/6/2007 10:51:00 AM

0

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.

Shuaib Zahda

11/6/2007 10:51:00 AM

0

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 helps
Cheers
--
Posted via http://www.ruby-....

Mario Ruiz

11/6/2007 10:56:00 AM

0

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

Peter Szinek

11/6/2007 11:03:00 AM

0

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.rubyra...
http://s...


Mario Ruiz

11/6/2007 11:16:00 AM

0

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

Peter Szinek

11/6/2007 11:33:00 AM

0

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.rubyra...
http://s...



Mario Ruiz

11/6/2007 11:41:00 AM

0

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?
--
Posted via http://www.ruby-....

Peter Szinek

11/6/2007 12:45:00 PM

0

Hi,

Mario Ruiz 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.rubyra...
http://s...




Warhol

2/26/2012 3:48:00 PM

0

On Feb 26, 1:56 pm, bill <blackuse...@gmail.com> wrote:
> On Sun, 26 Feb 2012 04:18:40 -0800, Warhol wrote:
> > On Feb 26, 11:05 am, bill <blackuse...@gmail.com> wrote:
> >> On Sun, 26 Feb 2012 01:21:53 -0800, Warhol wrote:
> >> > On Feb 25, 9:59 pm, bill <blackuse...@gmail.com> wrote:
> >> >> On Sat, 25 Feb 2012 12:33:28 -0800, Warhol wrote:
> >> >> > On Feb 25, 5:15 pm, bill <blackuse...@gmail.com> wrote:
> >> >> >> On Sat, 25 Feb 2012 04:41:53 -0800, Warhol wrote:
> >> >> >> > On Feb 25, 10:23 am, bill <blackuse...@gmail.com> wrote:
> >> >> >> >> On Fri, 24 Feb 2012 14:39:42 -0800, Warhol wrote:
> >> >> >> >> > On Feb 24, 10:42 pm, bill <blackuse...@gmail.com> wrote:
> >> >> >> >> >> On Fri, 24 Feb 2012 13:23:46 -0800, Warhol wrote:
> >> >> >> >> >> > On Feb 24, 10:09 pm, bill <blackuse...@gmail.com> wrote:
> >> >> >> >> >> >> On Fri, 24 Feb 2012 13:00:06 -0800, Warhol wrote:
> >> >> >> >> >> >> > On Feb 24, 9:45 pm, bill <blackuse...@gmail.com>
> >> >> >> >> >> >> > wrote:
> >> >> >> >> >> >> >> > They take an oath to uphold the constitution yet
> >> >> >> >> >> >> >> > they label those who believe the constitution is
> >> >> >> >> >> >> >> > the law of the land as terrorists. This is whats
> >> >> >> >> >> >> >> > called treason, now we need the states to unite and
> >> >> >> >> >> >> >> > go after the tyrannical federal government.
>
> >> >> >> >> >> >> >> Off you go then.
>
> >> >> >> >> >> >> >> let us all know how you get on...
>
> >> >> >> >> >> >> >> --
>
> >> >> >> >> >> >> > the crooks in Washington D.C. did take an oath to "A"
> >> >> >> >> >> >> > constitution but not the one you think. "They" have
> >> >> >> >> >> >> > changed or bastardized the original constitution and
> >> >> >> >> >> >> > these United Corporate States of Amerika is a business
> >> >> >> >> >> >> > (check the fine print folks) and Clinton, bush, Obama
> >> >> >> >> >> >> > are the ceo's of said corporation and are doing what
> >> >> >> >> >> >> > is expected of them from the Shareholders.
>
> >> >> >> >> >> >> Cut and paste bollocks.
>
> >> >> >> >> >> >> Next we'll get a religious tract.
>
> >> >> >> >> >> >> You're becoming predictable.
>
> >> >> >> >> >> >> --
>
> >> >> >> >> >> > Its funny that the government is losing the information
> >> >> >> >> >> > war.
>
> >> >> >> >> >> War?
>
> >> >> >> >> >> What war?
>
> >> >> >> >> >> > The reason that the sovereignty "movement" is growing
> >> >> >> >> >> > right now is the Internet.
>
> >> >> >> >> >> Bollocks.  It's more or less unknown outside of the USA,
> >> >> >> >> >> and there it's well known for kookery and people getting
> >> >> >> >> >> themselves locked away for refusing to pay their taxes.
>
> >> >> >> >> >> No tax,  no armed forces,  Cuba expands, Mexico takes back
> >> >> >> >> >> Texas, the British take back the 13 colonies...
>
> >> >> >> >> >> And don't give me that 'an armed society is a free society'
> >> >> >> >> >> rubbish because Afghanistan has a gun culture that makes
> >> >> >> >> >> the US look positively anaemic and look how far that's got
> >> >> >> >> >> them, they'll be entering the twelfth century any time
> >> >> >> >> >> now...
>
> >> >> >> >> >> --
>
> >> >> >> >> > Yeah, ain't that a conundrum, eh?
>
> >> >> >> >> > I actually prefer the term "anti-government extremestist"
> >> >> >> >> > that the FBI just started using. Much wider frame of people
> >> >> >> >> > lol...
>
> >> >> >> >> > They are name calling because it dehumanizes people. You
> >> >> >> >> > can't harm or kill anyone without dehumanizing them first.
>
> >> >> >> >> > Example: Man who murders wife often calls her bitch or whore
> >> >> >> >> > (or whatever name) to dehumanize her, this is the first
> >> >> >> >> > step.
>
> >> >> >> >> > Anyone who defends the constitution, wants to end the fed,
> >> >> >> >> > wants to bring back the republic will be considered a
> >> >> >> >> > terrorist. I wonder why they haven't arrested Ron Paul and
> >> >> >> >> > his group. The movement has several million members. They
> >> >> >> >> > are not right wing nuts. Most of them are well educated.
> >> >> >> >> > Just a bunch of good people doing good things.
>
> >> >> >> >> Nope,  just nasty chancers who don't want to pay any taxes.
>
> >> >> >> >> --
>
> >> >> >> > why should I pay for drones to shot people or for wars that
> >> >> >> > only serve the bankers, or for roads that only serve those who
> >> >> >> > have cars, or pay for organization that censure my words...
> >> >> >> > give me only 1 reason why I should pay for things that don't
> >> >> >> > serve me or my siblings.
>
> >> >> >> You live in a free country,  you can move.
>
> >> >> >> Somalia welcomes people who wish to live free.
>
> >> >> >> So does rural Afghanistan...
>
> >> >> >> See how long you last in a  country where there are no taxes...
>
> >> >> >> --
>
> >> >> > A family in North Dakota that were members of the “Sovereign
> >> >> > Citizens” movement became in June the first American citizens in
> >> >> > history to be arrested with the help of Predator drone aircraft,
> >> >> > according to a report published Monday morning.
>
> >> >> >http://www.rawstory.com/rs/2011/12/12/sovereign-citizen...
>
> >> >> arrested-with-help-of-predator-drone/
>
> >> >> Let them move to Somalia...
>
> >> >> --
>
> >> >  and why not to WYOMING?...
>
> >> Because they have a government and governments cost money and they
> >> raise this money by making people pay taxes.
>
> >> --
>
> > In Somalia also there is government... so you dont have any point
> > again...
>
> In Somalia they have a government but its writ doesn't run outside their
> capital city, same as Afghanistan...
>
> Rural farmers don't pay taxes anywhere in the developing world,  but I'd
> be willing to bet that this bunch wouldn't last five minutes out from
> under Uncle Sam's umbrella.
>
>
>
> > but did you understand the message of my post... I dont think so...
> > otherwise you would have understood the system you protect is falling at
> > free fall... and when they fall they will blame the jews and
> > rothchilds...
>
> They usually do.
>
> But these days the Jews have guns as well...
>
> The days when the Jews died quietly when swine like you started shooting
> are long gone.
>
> > in camps is where yids belong...
>
> You are welcome to try putting them into some.  We'll see what happens to
> you if you try.
>
> Interestingly the Rothschild family discovered this very early and have
> been associated with the British military for about 200 years now.
>
> --


Nah, JEWS are too cowardly to fight...

My GranDa'Dy, your G-D has nothing to do with you. HE will crush you
and send you to hell where you belong...

THERE IS NOTHING YOU CAN DO TO STOP DESTINY

We want Freedom, the one our God Creator gave us.

Besides that, I think you´re a shill.

You have to start looking for another job soon, fucktard