From: "Hal E. Fulton" Date: 2003-06-18T08:01:53+09:00 Subject: Re: [RCR] Array#push(empty array expanded) => no exception ----- Original Message ----- From: "ahoward" Newsgroups: comp.lang.ruby To: "ruby-talk ML" Sent: Tuesday, June 17, 2003 4:07 PM Subject: Re: [RCR] Array#push(empty array expanded) => no exception > > For one thing, what if a variable is passed in? We don't want it to fail > > just because the array is empty. > > well this is not *exactly* the case simon described though is it? > > method [] # empty array as arg > method *[] # NO args! Well, I meant more like this case: myvar = mymethod # mymethod returns an array... possibly empty meth2(*myvar) # Pass that array as individual args > so, it's not that the method is failing just because an empty array is passed > in, it is failing because an empty array was exploded, yielding and > non-existent parameter list. i may be in the minority, but it's not THAT > counter intuitive for me that attempting to call a method expecting args, with > no args, should be an error. The asterisk (or star or splat) is a special case. I can't think of a case where Ruby generates a "number of params" type of error for a method where star is specified where the method is defined. On the other hand, I am simply *assuming* that there is a star on the definition. There certainly is one on the call... they usually go together, UNLESS you are expanding an array of known size, resulting in exactly N parameters. > > For another thing, consider what happens with > > strings. foo = "value" + "" should not be > > illegal, I think. > > no, of course not. but this is analogous to the first example above, not the > second one, which would be more along the lines of > > foo = "value" + *"" > > if '*String' did, for example, an explosion into a list of Fixnums (chars) or > something like that... Well, I see what you mean. But they are analogous in this sense: Given a collection of entities, add another entity that is a "null" with respect to the context of that collection and its add operation. Pushing an empty list of items onto an array doesn't change the array, just as adding a null string onto a string doesn't change that string; except that we are actually doing an assignment where the string is concerned -- better to make it a <<. Boy, this is getting more and more nitpicky, isn't it? The bottom line, to me, is that if there is a star in the definition AND in the caller, the idea of "expanding" the array is almost meaningless. Is there ever a real difference in these two situations? (See below.) There probably is something I'm overlooking, of course. def meth1(*args) #... end meth1(*myarray) def meth2(args) #... end meth2(myarray) Cheers, Hal