Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.ruby > #4105
| From | 7stud -- <bbxx789_05ss@yahoo.com> |
|---|---|
| Newsgroups | comp.lang.ruby |
| Subject | Re: Threading Loops |
| Date | 2011-05-07 21:54 -0500 |
| Organization | Service de news de lacave.net |
| Message-ID | <189bf3bfbdfcc3f924515136b5836617@ruby-forum.com> (permalink) |
| References | <670f5be69524ccbfc188e9062e65f645@ruby-forum.com> <e8e68ccf9f0ae114371b54b536f1ee23@ruby-forum.com> |
Bobby S. wrote in post #997293: > Thank you so much that helped allot and now I understand threading > better. > > Could you explain the first part? > pic_names.collect.with_index do |name, pic_numb| > > I understand how the threading is working but not the loop. I read > .collect returns all elements in an array. But what about the > with_index and adding pic_numb to the iterator. In ruby 1.9, if you call collect() without supplying a block, you get what's called an 'enumerator' back. It's an object of the class Enumerator, which has a method called with_index(). with_index() works just like each()--but it sends a second argument to the block: the index of the element. I don't like that collect() loop at all. collect() returns an array containing elements of the original array for which the block evaluates to true. But the only thing inside the block is Thread.new(), which always returns something that evaluates to true, so all elements of the original array are selected by collect() and returned in a new array, which is then discarded because the result of collect() isn't assigned to a variable. So, why not just use each(), which does not return a new array: arr = ['a', 'b', 'c'] arr.each.with_index do |el, index| p [el, index] end --output:-- ["a", 0] ["b", 1] ["c", 2] -- Posted via http://www.ruby-forum.com/.
Back to comp.lang.ruby | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
Threading Loops "Bobby S." <kajisakka@gmail.com> - 2011-05-07 18:20 -0500
Re: Threading Loops Roy Zuo <roylzuo@gmail.com> - 2011-05-07 18:42 -0500
Re: Threading Loops "Bobby S." <kajisakka@gmail.com> - 2011-05-07 19:03 -0500
Re: Threading Loops 7stud -- <bbxx789_05ss@yahoo.com> - 2011-05-07 21:54 -0500
Re: Threading Loops Robert Klemme <shortcutter@googlemail.com> - 2011-05-08 12:53 +0200
Re: Threading Loops "Bobby S." <kajisakka@gmail.com> - 2011-05-07 20:04 -0500
Re: Threading Loops "Bobby S." <kajisakka@gmail.com> - 2011-05-07 20:48 -0500
Re: Threading Loops 7stud -- <bbxx789_05ss@yahoo.com> - 2011-05-07 21:16 -0500
Re: Threading Loops Robert Klemme <shortcutter@googlemail.com> - 2011-05-08 12:56 +0200
Re: Threading Loops 7stud -- <bbxx789_05ss@yahoo.com> - 2011-05-07 20:56 -0500
Re: Threading Loops Christopher Dicely <cmdicely@gmail.com> - 2011-05-07 22:36 -0500
Re: Threading Loops Kevin Bullock <kbullock+rubyforum@ringworld.org> - 2011-05-07 21:27 -0500
Re: Threading Loops "Bobby S." <kajisakka@gmail.com> - 2011-05-07 22:12 -0500
Re: Threading Loops Brian Candler <b.candler@pobox.com> - 2011-05-08 09:07 -0500
csiph-web