From: Benoit Daloze Date: 2009-12-20T08:28:43+09:00 Subject: Re: Splat array with 1 value in Ruby 1.9 vs Ruby 1.8 --0016364175276708b8047b1d37c7 Content-Type: text/plain; charset=ISO-8859-1 Humm, well, it has a very-not-easy-to-see link, but it is. I imagined at a moment using the splat operator, while it can of course not work without any variable. I use code above to allow dynamic parameters based on the name of the variable who's result of that method is assigned to that variable. (for exemple : x = var allow to pass a parameter "x" to the method var() ) So I'm *scan*ning the code (probably not a good idea but ...) and then I want this method to return an array if I do: x, y = var or a single object like above. Ok, I'll just keep the ".length" code ... but I asked if there was a feature on Array acting like a splat operator. class Array def splat length == 1 ? shift : self end end (then in 1.8, I imagined to do: "*result" at the last line of the method. irb(main):001:0> def m irb(main):002:1> arr = [2] irb(main):003:1> *arr irb(main):004:1> end SyntaxError: compile error (irb):3: syntax error, unexpected '\n', expecting '=' from (irb):4 irb(main):005:0> def m irb(main):006:1> arr = [2] irb(main):007:1> return *arr irb(main):008:1> end => nil irb(main):009:0> m => 2 For once, return is needed it seems ...) 2009/12/19 Raul Parolari > Benoit Daloze wrote: > > Well, so what's the solution to get the single element in the array or > > the > > whole array when multiple elements ? > .. > > how to make this in a beautiful way? > > I would like to have only the element if there is only one > > > > of course, there is: > > arr = "str".scan(/.../) > > arr.length == 1 ? arr[0] : arr > > > > but that create a useless new variable and doesn't look good at all. > > > > Benoit, str.scan(pattern) always returns an array of matches; you then > need to process the resulting array (for example, iterating on it, > extracting the elements of the array). You seem to want a different > result depending if the array size is 1 or not (that is by the way the > opposite of what this post was about, on a different problem). > > I find that suspect; think about it: from that moment on, the rest of > the program will have to deal with a result that is of different type > depending on the fact that there was only 1 value or not in the array? > this is odd, and it is usually the contrary of what you want. > Anyhow, if you want to do such a thing, yes, test for array size and > extract the value (with shift or [0], etc). And at that point, you will > happily have a scalar value or an array depending on the number of > elements in the original array. > > --- > For clarity: > the original problem discussed in the thread was this: the splat > operator does not do what it used to when there is only 1 element in the > array. Robert Klemme has indicated above a solution to this (using the > 'comma' notation at the end of left values). I posted a bug against > Ruby 1.9. > This problem posted by Benoit above has nothing to do with this. > > > -- > Posted via http://www.ruby-forum.com/. > > --0016364175276708b8047b1d37c7--