Todd Benson
11/5/2008 10:47:00 PM
On Wed, Nov 5, 2008 at 7:08 AM, Jags Rao <aquajags@yahoo.com> wrote:
> hi guys
>
> i have a array like this for e.g
>
> [["0", "0", "0", "0", "0", "3", "3"],
> ["0", "0", "0", "0", "0", "3", "3"],
> ["0", "0", "0", "0", "0", "3", "3"],
> ["0", "3", "0", "0", "0", "0", "3"],
> ["0", "0", "0", "0", "0", "0", "3"]]
>
> how wud i convert to this
>
> [[0, 0, 0, 0, 0, 3, 3],
> [0, 0, 0, 0, 0, 3, 3],
> [0, 0, 0, 0, 0, 3, 3],
> [0, 0, 0, 0, 0, 3, 3],
> [0, 0, 0, 0, 0, 0, 3],
> [0, 0, 0, 0, 0, 0, 3],
> [0, 3, 0, 0, 0, 0, 3],
> [0, 0, 0, 0, 0, 0, 3],
>
> i.e a string array to integer array in 1 line if possible
Here's a weird one for your enjoyment (arr is your array)...
require 'matrix'; p Matrix[*arr].map {|i| i.to_i}.to_a
...of course, just like Stefano's solution, the object must respond to
the #to_i method.
You can #join it and split it up (#each_slice) again (my first cool
solution, but looks _really_ ugly). Mapping twice is the easiest and
is the way you probably should go.
Todd