From: 7stud -- Date: 2011-05-08T11:54:38+09:00 Subject: Re: Threading Loops 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/.