Shortcut for x = [x] unless x.is_a?(Array)

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

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

···

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.