Reputation: 1074
I want to create an array of arrays from another array:
a = [11,1,[23,21],14,[90,1]]
a.map { |e| e.is_a?(Array) ? e : [e] }
# => [[11], [1], [23, 21], [14], [90, 1]]
Is there an elegant way to do this?
Upvotes: 0
Views: 71
Reputation: 118261
I would do as below :
a = [11,1,[23,21],14,[90,1]]
a.map { |e| [*e] }
# => [[11], [1], [23, 21], [14], [90, 1]]
or using Kernel#Array()
a.map { |e| Array(e) }
# => [[11], [1], [23, 21], [14], [90, 1]]
Use, which ever you think as elegant, for me both are elegant :-)
Upvotes: 6