On 23 May 2013 13:52, jsnark <[email protected]> wrote:
> Write a loop that generates the desired indexes and then reference arr:
>
> (0..5).each do |j|
>   i = 10*j
>   puts "#{arr[i]} at #{i}"
> end
>
> Your example output does not match the code.  It does not include 0, 20, and
> 40.

I don't think the OP wanted 20 and 40 which is why he is adding 10
rather than 9.  I suspect the example is just an example and does not
represent the actual code he wants, in particular there is likely to
be an else block, and it that which he wishes to skip.  I think the
answer to his question, however, is that there is no way to change the
index within the block in order to skip elements, so he will have to
use one of the suggestions made by others.

Colin

>
>
> On Thursday, May 23, 2013 5:24:36 AM UTC-4, Ruby-Forum.com User wrote:
>>
>> Can anyone help me on how to skip index in ARRAY.each_with_index loop?
>>
>> Please look at below case:
>>
>> In a ARRAY.each_with_index loop, I would like increase the index by 10
>> whenever a condition is met.
>>
>> arr is array of numbers from 0 to 50
>> arr.each_with_index do |x,i|
>>   if i % 10 == 0
>>     puts "#{x} at #{i}"
>>     i = i+10  //index increased by 10
>>   end
>> end
>>
>> The output should be:
>> 10 at 10
>> 30 at 30
>> 50 at 50
>>
>> Thanks
>> Ajit
>>
>> --
>> Posted via http://www.ruby-forum.com/.
>
> --
> You received this message because you are subscribed to the Google Groups
> "Ruby on Rails: Talk" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to [email protected].
> To post to this group, send email to [email protected].
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/rubyonrails-talk/6cb2b3e6-32c3-4f5d-b85a-fdfa1c5427f4%40googlegroups.com?hl=en-US.
>
> For more options, visit https://groups.google.com/groups/opt_out.
>
>

-- 
You received this message because you are subscribed to the Google Groups "Ruby 
on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/rubyonrails-talk/CAL%3D0gLvR8rUqPfaamjtW%3DQy9T2TXdQMvEO44Y_JE31rbpsq0HQ%40mail.gmail.com?hl=en-US.
For more options, visit https://groups.google.com/groups/opt_out.


Reply via email to