[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

Re: shortcut for x = [x] unless x.is_a?(Array

Jeremy Weiskotten

3/11/2008 11:32:00 PM

Aryk Grosz wrote:
> Is there any prettier or cleaner way to write
>
> x = [x] unless x.is_a?(Array)

x = [x].flatten

Although I suspect the x = Array(x) method would be faster.
--
Posted via http://www.ruby-....

1 Answer

Todd Benson

3/12/2008 1:01:00 AM

0

On Tue, Mar 11, 2008 at 6:32 PM, Jeremy Weiskotten
<jeremy.weiskotten@gmail.com> wrote:
> Aryk Grosz wrote:
> > Is there any prettier or cleaner way to write
> >
> > x = [x] unless x.is_a?(Array)
>
> x = [x].flatten
>
> Although I suspect the x = Array(x) method would be faster.

Yeah, but those are a little different, too...

x = [[1, 2, 3], [1, 2]]
p x.flatten

=> [1, 2, 3, 1, 2]

x = [[1, 2, 3], [1, 2]]
p Array(x)

=> [[1, 2, 3], [1, 2]]

Todd