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