--mP3DRpeJDSE+ciuQ
Content-Type: text/plain; charset=us-ascii
Content-Disposition: inline
Content-Transfer-Encoding: quoted-printable

On Sun, Apr 03, 2011 at 03:32:23AM +0900, 7stud -- wrote:
> Kyle X. wrote in post #990532:
> >
> > 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

. . . or, for a one-line version:

    transformed_data = data.map {|arr| arr.map {|int| int.to_f } }

I don't recommend trying to fit everything into one line, of course.
Spreading things out a bit can make them easier to understand later,
sometimes.  You should (pretty much) always judge how you organize your
code by how easily another programmer will be able to understand it, even
if that other programmer is you six months later after having forgotten
what you did when you first wrote it.


> 
> 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.

What he said.  You can read more about it (and other Ruby Array methods)
using either ri or Google (of course).  For instance, part of what you
get from `ri Array` is:

    Includes:
    ---------
         Enumerable(all?, any?, collect, count, cycle, detect, drop,
         drop_while, each_cons, each_slice, each_with_index, entries,
         enum_cons, enum_slice, enum_with_index, find, find_all, find_index,
         first, grep, group_by, include?, inject, inject, map, max, max_by,
         member?, min, min_by, minmax, minmax_by, none?, one?, partition,
         reduce, reject, reverse_each, select, sort, sort_by, take,
         take_while, to_a, to_set, zip)

. . . and you can use ri to find out about each of them, such as by
asking it about map with `ri Array.map`.  In Google, I find that doing a
search for something like "ruby array" (without quotes) basically always
gives me the documentation for the appropriate class as the first hit.
For "ruby array", I get this as the first hit:

    http://www.ruby-doc.org/core/classes/Array.html

Sometimes, solving a problem in writing Ruby code is as simple as
checking out method lists like those provided by ri and Google to remind
myself about a method I forgot I had at my disposal.

-- 
Chad Perrin [ original content licensed OWL: http://owl.apotheon.org ]

--mP3DRpeJDSE+ciuQ
Content-Type: application/pgp-signature
Content-Disposition: inline

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v2.0.14 (FreeBSD)

iEYEARECAAYFAk2X1fMACgkQ9mn/Pj01uKVI8wCgqzCU1kRQe87mLm/IJsGLXdmN
sPAAn3Fdq7v0+sE0oyZYpfhnGOWUr2O1
lX
-----END PGP SIGNATURE-----

--mP3DRpeJDSE+ciuQ--