Shpigford
Shpigford

Reputation: 25368

Reduce nested array to string?

I have the following array:

arr = [["Example"]]

I need to reduce it to just "Example" (basically, just remove the array).

I know I could do arr[0][0], but am curious if there's a simple method to just remove the string from the array without using indexes.

For clarification...there will only ever be a single item in the array.

Upvotes: 1

Views: 486

Answers (2)

MurifoX
MurifoX

Reputation: 15089

For a single item, you can use:

[['array']].join
=> 'array'

Updated with more examples

If you have multiple items, the strings will be combined:

[['array'], ['array']].join
=> 'arrayarray'

And if you pass a parameter to the join method:

[['array'], ['array']].join('&')
=> 'array&array'

Upvotes: 7

Linuxios
Linuxios

Reputation: 35788

While this is not as efficient as [0][0], it will still work:

arr.flatten.first

Upvotes: 2

Related Questions