Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.ruby > #2019

Re: How can I generate new variables?

From "Kyle X." <haebooty@yahoo.com>
Newsgroups comp.lang.ruby
Subject Re: How can I generate new variables?
Date 2011-03-30 22:08 -0500
Organization Service de news de lacave.net
Message-ID <ddc92babca17a6f4dc20fcb6fef168b5@ruby-forum.com> (permalink)
References <33ba41ea1d06d357f353d2462a951026@ruby-forum.com> <bec536840ae5b1c75ae0d824c20af7d3@ruby-forum.com>

Show all headers | View raw


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

Back to comp.lang.ruby | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

How can I generate new variables? "Kyle X." <haebooty@yahoo.com> - 2011-03-30 19:42 -0500
  Re: How can I generate new variables? 7stud -- <bbxx789_05ss@yahoo.com> - 2011-03-30 20:14 -0500
    Re: How can I generate new variables? "Kyle X." <haebooty@yahoo.com> - 2011-03-30 22:08 -0500
      Re: How can I generate new variables? Gunther Diemant <g.diemant@gmx.net> - 2011-03-31 03:08 -0500
        Re: How can I generate new variables? "Kyle X." <haebooty@yahoo.com> - 2011-03-31 14:30 -0500
      Re: How can I generate new variables? Robert Klemme <shortcutter@googlemail.com> - 2011-03-31 03:11 -0500
      Re: How can I generate new variables? 7stud -- <bbxx789_05ss@yahoo.com> - 2011-03-31 20:06 -0500
  Re: How can I generate new variables? "Kyle X." <haebooty@yahoo.com> - 2011-04-02 01:18 -0500
    Re: How can I generate new variables? Robert Klemme <shortcutter@googlemail.com> - 2011-04-04 02:15 -0500

csiph-web