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

@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 Thu, Oct 7, 2010 at 12:41 AM, Stephen B. [email protected]
wrote:

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

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

([email protected]).each {|i|
}

or

for i in [email protected]
end

Kind regards

robert

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs