简体   繁体   English

在Ruby上使用.each方法循环

[英]Loop with .each method on ruby

I have a doubt, i have a .each loop like this: 我有一个疑问,我有一个.each循环是这样的:

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 该循环从数组@lt的第一项开始,但我希望它从第二项开始

Assuming that @lt is an Enumerable (Array, Hash, or similar), you can use drop to skip any number of items from the start: 假设@lt是可Enumerable (数组,哈希或类似名称),则可以使用drop从头开始跳过任何数量的项目:

@lt.drop(1).each do |item|
...
end
@lt[1..-1].each do |item|
  # Do things with item
end
@lt[1..(@lt.length-1)].each { |item|
   # Do something
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM