From: "Kyle X." Date: 2011-03-31T12:08:02+09:00 Subject: Re: How can I generate new variables? First off thank you for the reply, and as you can probably tell I am very novice at ruby ><. However I am running into a few issues with this. > Using arrays. An array can contain anything--including other arrays: > > > require 'enumerator' #not necessary in ruby 1.9 > > master_arr = [] > data = [1, 2, 3, 4, 5, 6, 7, 8] > > data.each_slice(3) do |triplet| > master_arr << triplet > end > > p master_arr > > --output:-- > [[1, 2, 3], [4, 5, 6], [7, 8]] I am writing this for SketchUP so I am using Ruby 1.8.6 and am having an issue with require 'enumerator', as it does not exist in their library as far as I can tell --- in turn the "each_slice" command does not function. I was wondering if you could help me understand this command better as well. "data.each_slice(3) do |triplet|" The "(3)" here means slice after 3 entries in data correct? The word triplet used, is this word required or could it be any word as long as it is consistent below i.e. : do |x| master_arr << x? Sorry for the basic question, but I just want to clarify. > > The names of the arrays are: > > master_arr[0] > master_arr[1] > master_arr[2] > > Voila! But in ruby, you rarely need to refer to index positions in an > array because you can do this: > > master_arr.each do |arr| > p arr > end > > --output:-- > [1, 2, 3] > [4, 5, 6] > [7, 8] > > Or this: > > master_arr.each do |arr| > new_arr = arr.map do |element| > element * 2 > end > > p new_arr > end > > --output:-- > [2, 4, 6] > [8, 10, 12] > [14, 16] The rest I think I have figured out, but still not 100% on the map function. Thank you again for your time. -- Posted via http://www.ruby-forum.com/.