user3407533
user3407533

Reputation: 59

Loop with .each method on ruby

I have a doubt, i have a .each loop like this:

mayor = @lt[1][1]
@lt.each do |item|
  if item[1] > mayor then
    mayor = item[1]
  end
end

That loop started in the first item of the array @lt but instead i want it to start by the second item

Upvotes: 0

Views: 72

Answers (3)

rthbound
rthbound

Reputation: 1343

@lt[1..-1].each do |item|
  # Do things with item
end

Upvotes: 1

ste
ste

Reputation: 411

@lt[1..(@lt.length-1)].each { |item|
   # Do something
}

Upvotes: 0

PinnyM
PinnyM

Reputation: 35533

Assuming that @lt is an Enumerable (Array, Hash, or similar), you can use drop to skip any number of items from the start:

@lt.drop(1).each do |item|
...
end

Upvotes: 3

Related Questions