Path: csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!news.dougwise.org!nntpfeed.proxad.net!proxad.net!feeder1-2.proxad.net!usenet-fr.net!de-l.enfer-du-nord.net!feeder2.enfer-du-nord.net!feeds.phibee-telecom.net!talisker.lacave.net!lacave.net!not-for-mail From: 7stud -- Newsgroups: comp.lang.ruby Subject: Re: Simple array.each do |x| question Date: Sat, 2 Apr 2011 13:32:23 -0500 Organization: Service de news de lacave.net Lines: 45 Message-ID: <2b98c82b7bbb2ec7c8c0e99b7d0efe84@ruby-forum.com> References: <9d19e7d38f2f3b6ab99b3150717a307c@ruby-forum.com> <6f6bb08b25ceb72409f83615448a82b6@ruby-forum.com> <20110401225432.GB86915@guilt.hydra> <4ed2ae7acfe8cdab6fbb0fbff47e3e65@ruby-forum.com> NNTP-Posting-Host: bristol.highgroove.com Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 7bit X-Trace: talisker.lacave.net 1301769164 2842 65.111.164.187 (2 Apr 2011 18:32:44 GMT) X-Complaints-To: abuse@lacave.net NNTP-Posting-Date: Sat, 2 Apr 2011 18:32:44 +0000 (UTC) In-Reply-To: <4ed2ae7acfe8cdab6fbb0fbff47e3e65@ruby-forum.com> X-Received-From: This message has been automatically forwarded from the ruby-talk mailing list by a gateway at comp.lang.ruby. If it is SPAM, it did not originate at comp.lang.ruby. Please report the original sender, and not us. Thanks! For more details about this gateway, please visit: http://blog.grayproductions.net/categories/the_gateway X-Mail-Count: 380806 X-Ml-Name: ruby-talk X-Rubymirror: Yes X-Ruby-Talk: <2b98c82b7bbb2ec7c8c0e99b7d0efe84@ruby-forum.com> Xref: x330-a1.tempe.blueboxinc.net comp.lang.ruby:2159 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/.