Tuesday, June 21, 2011

How to revert the flatten value of an array ?

 a = [[1,2],[3,4],[5,6],[7,8]]
a.flatten
[1, 2, 3, 4, 5, 6, 7, 8]

now if you want to revert the fatten value
a= [1, 2, 3, 4, 5, 6, 7, 8]
a.inject([]){|result, element| result << [element, a[a.index(element)+1]] if a.index(element) %2 == 0; result }

[[1,2],[3,4],[5,6],[7,8]]



No comments:

Post a Comment