Looping through array starting at i !=0

Hi everyone,

I want to loop through this array, but not for every index.. only
indices that are
greater than 2.

    @weights.each_index do |i|
end

Ted

···

--
Posted via http://www.ruby-forum.com/.

@weights[2..-1].each_index{|i|
}

or

@weights.each_index{|i|
next if i<2
code
}

I think the first version makes a new object.
Becker

···

On Wed, Oct 6, 2010 at 3:08 PM, John Smith <edvelez.g@gmail.com> wrote:

Hi everyone,

I want to loop through this array, but not for every index.. only
indices that are
greater than 2.

   @weights.each_index do |i|
end

Ted
--
Posted via http://www.ruby-forum.com/\.

That would be

@weights[3..-1].each_index{|i|
}

(OP said "greater than 2".)

But it does not work because each_index will start at 0. Rather you
probably meant

@weights[3..-1].each {|elem|
}

For the indexes only one can do

(3...@weights.size).each {|i|
}

or

for i in 3...@weights.size
end

Kind regards

robert

···

On Thu, Oct 7, 2010 at 12:41 AM, Stephen Becker <becker@qagist.com> wrote:

@weights[2..-1].each_index{|i|
}

--
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/