looping through array starting at i !=0

J

John Smith

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
 
S

Stephen Becker

[Note: parts of this message were removed to make it a legal post.]

@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
 
R

Robert Klemme

@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 (e-mail address removed)
end

Kind regards

robert
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,042
Latest member
icassiem

Latest Threads

Top