Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.ruby > #2076
| Path | csiph.com!x330-a1.tempe.blueboxinc.net!newsfeed.hal-mli.net!feeder1.hal-mli.net!feeder.news-service.com!feeds.phibee-telecom.net!talisker.lacave.net!lacave.net!not-for-mail |
|---|---|
| From | 7stud -- <bbxx789_05ss@yahoo.com> |
| Newsgroups | comp.lang.ruby |
| Subject | Re: How can I generate new variables? |
| Date | Thu, 31 Mar 2011 20:06:52 -0500 |
| Organization | Service de news de lacave.net |
| Lines | 118 |
| Message-ID | <2e3e85f3b2c7c85b6a4ae66e5c415836@ruby-forum.com> (permalink) |
| References | <33ba41ea1d06d357f353d2462a951026@ruby-forum.com> <bec536840ae5b1c75ae0d824c20af7d3@ruby-forum.com> <ddc92babca17a6f4dc20fcb6fef168b5@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 1301620059 37513 65.111.164.187 (1 Apr 2011 01:07:39 GMT) |
| X-Complaints-To | abuse@lacave.net |
| NNTP-Posting-Date | Fri, 1 Apr 2011 01:07:39 +0000 (UTC) |
| In-Reply-To | <ddc92babca17a6f4dc20fcb6fef168b5@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 | 380722 |
| X-Ml-Name | ruby-talk |
| X-Rubymirror | Yes |
| X-Ruby-Talk | <2e3e85f3b2c7c85b6a4ae66e5c415836@ruby-forum.com> |
| Xref | x330-a1.tempe.blueboxinc.net comp.lang.ruby:2076 |
Show key headers only | View raw
Kyle X. wrote in post #990113:
> First off thank you for the reply, and as you can probably tell I am
> very novice at ruby >
No problem--that's what this forum is for.
>>
>> 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
>
I'm using ruby 1.8.6 too, and the Enumerator module is part of the ruby
standard library. Are you sure you spelled it right? But if it's not
provided, it's not provided.
> 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?
Yes, the each_slice() method allows you to chop up an array into chunks
of the specified size.
> 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.
>
'triplet' is just a descriptive variable name. It is a "block
parameter" which is just like a method parameter:
def do_stuff(x) #x is a method parameter
x+2 #or you can explicitly write: return x+2
end
answer = do_stuff(5)
puts answer #=> 7
Ruby uses blocks everywhere. Here is a simple example:
words = ["hello", "world", "goodbye"]
words.each do |word|
puts word
end
--output:--
hello
world
goodbye
The each() method sends each element of the array to the block, which is
this part:
do |word|
puts word
end
'do' signals the start of a block. What happens is that ruby calls the
block, which is like a method with, for each element of the array. In
my example, the element of the array is assigned to the variable named
'word', and then inside the block you can do whatever you want to the
word variable.
The map() function, like each(), tells ruby to send each element of the
array to the block. However, map() also *takes the return value of the
block* and shoves it into a new array:
words = ["hello", "world", "goodbye"]
new_arr = words.map do |x|
1
end
p new_arr
--output:--
[1, 1, 1]
In that example, the block returns 1 for every element of the array.
But you can use map to do more useful things--like capitalize all the
words in the array:
words = ["hello", "world", "goodbye"]
new_arr = words.map do |x|
x.capitalize #same as: 'return x.capitalize'
end
p new_arr
--output:--
["Hello", "World", "Goodbye"]
So whenever, you want to do something to every member of an array, map()
is a good choice.
--
Posted via http://www.ruby-forum.com/.
Back to comp.lang.ruby | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll 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