#### Topic: what is the difference between these two?

what is the difference between these?

``````x = ["blah"]
[x].flatten``````

vs.

``````x = ["blah"]
[*x]``````

-patrick

Last edited by patrick99e99 (2010-01-09 18:56:51)

#### Re: what is the difference between these two?

By Ruby API, flatten Returns a new array that is a one-dimensional flattening of this array (recursively). That is, for every element that is an array, extract its elements into the new array.

``````irb(main):009:0> arr =[1,2,3,[1,2,3,4,5,6,7,8,9,10]]
=> [1, 2, 3, [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]]
irb(main):011:0> arr.flatten!
=> [1, 2, 3, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]``````

What does your [*arr] version, I don't know, in the irb it didn't change anything anyway:

``````irb(main):010:0> [*arr]
=> [1, 2, 3, [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]]``````

#### Re: what is the difference between these two?

ok...  So, I see then.. I should have been thinking more like how methods take that splat param like

``````def something(*args)
end``````

When I am doing:

``````>> arr = [1,2,3]
=> [1, 2, 3]
>> [arr]
=> [[1, 2, 3]]
>> [*arr]
=> [1, 2, 3]``````

I'm really saying, build a new array with the items inside arr..  Similar to doing

``````arr.inject([]) {|a,v| a << v}

# or simply

arr.map``````

-patrick