From: 7stud -- Date: 2011-04-03T03:32:23+09:00 Subject: Re: Simple array.each do |x| question Kyle X. wrote in post #990532: > > So once I flatten the array it would become - > [0,1,2,3,4,5,6,7,8] from > [[0,1,2],[3,4,5],[6,7,8]] correct? > Yes, and you can always test that. If you use: p some_array ruby will output the array structure. > This would then allow me to all in one .to_f, yes? Would this be > possible to do without flattening, or is it not possible with arrays in > an array? > You wouldn't want to flatten in that case. Instead you can use some nested map() calls: data = [[0,1,2],[3,4,5],[6,7,8]] transformed_data = data.map do |arr| arr.map do |int| int.to_f end end p transformed_data --output:-- [[0.0, 1.0, 2.0], [3.0, 4.0, 5.0], [6.0, 7.0, 8.0]] map() sends each element of the data array to the block parameter arr. Because each element of the data array is a sub-array, I named the block parameter 'arr' to indicate that. Now that you have a reference to the sub-array inside the outer block, you can apply another map() to the subarray to do the actual transformation. The inner map() block returns a new array that has transformed one of the data sub-arrays to floats. And the outer map stuffs each of those returned-transformed-arrays into a new array--producing the final result. -- Posted via http://www.ruby-forum.com/.