相当于“继续”。在红宝石中

发布于 2024-09-29 14:22:29 字数 117 浏览 8 评论 0原文

在 C 和许多其他语言中,有一个 continue 关键字,当在循环内部使用时,会跳转到循环的下一次迭代。 Ruby 中是否有与此 continue 关键字等效的内容?

In C and many other languages, there is a continue keyword that, when used inside of a loop, jumps to the next iteration of the loop. Is there any equivalent of this continue keyword in Ruby?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(9

海螺姑娘 2024-10-06 14:22:29

是的,它叫做next

for i in 0..5
   if i < 2
     next
   end
   puts "Value of local variable is #{i}"
end

输出如下:

Value of local variable is 2
Value of local variable is 3
Value of local variable is 4
Value of local variable is 5
 => 0..5 

Yes, it's called next.

for i in 0..5
   if i < 2
     next
   end
   puts "Value of local variable is #{i}"
end

This outputs the following:

Value of local variable is 2
Value of local variable is 3
Value of local variable is 4
Value of local variable is 5
 => 0..5 
知你几分 2024-10-06 14:22:29

next

另外,查看 redo,它重做当前迭代。

next

also, look at redo which redoes the current iteration.

小鸟爱天空丶 2024-10-06 14:22:29

以稍微更惯用的方式编写Ian Purton的答案

(1..5).each do |x|
  next if x < 2
  puts x
end

打印:

  2
  3
  4
  5

Writing Ian Purton's answer in a slightly more idiomatic way:

(1..5).each do |x|
  next if x < 2
  puts x
end

Prints:

  2
  3
  4
  5
为你鎻心 2024-10-06 14:22:29

在 for 循环和迭代器方法(例如 eachmap)中,ruby 中的 next 关键字将具有跳转到循环的下一次迭代的效果(与 C 中的继续相同)。

然而它实际上所做的只是从当前块返回。因此,您可以将它与任何采用块的方法一起使用 - 即使它与迭代无关。

Inside for-loops and iterator methods like each and map the next keyword in ruby will have the effect of jumping to the next iteration of the loop (same as continue in C).

However what it actually does is just to return from the current block. So you can use it with any method that takes a block - even if it has nothing to do with iteration.

无所谓啦 2024-10-06 14:22:29

Ruby 还有另外两个循环/迭代控制关键字:redoretry
在 Ruby QuickTips 中了解有关它们以及它们之间差异的更多信息

Ruby has two other loop/iteration control keywords: redo and retry.
Read more about them, and the difference between them, at Ruby QuickTips.

七月上 2024-10-06 14:22:29

我认为它被称为下一个

I think it is called next.

许一世地老天荒 2024-10-06 14:22:29

使用可以使用下一个条件

before = 0
"0;1;2;3".split(";").each.with_index do |now, i|
    next if i < 1
    puts "before it was #{before}, now it is #{now}"
    before = now
end

输出:

before it was 0, now it is 1
before it was 1, now it is 2
before it was 2, now it is 3

Use may use next conditionally

before = 0
"0;1;2;3".split(";").each.with_index do |now, i|
    next if i < 1
    puts "before it was #{before}, now it is #{now}"
    before = now
end

output:

before it was 0, now it is 1
before it was 1, now it is 2
before it was 2, now it is 3
梦里人 2024-10-06 14:22:29

使用 next,它将绕过该条件,其余代码将起作用。
下面我提供了完整的脚本和输出

class TestBreak
  puts " Enter the nmber"
  no= gets.to_i
  for i in 1..no
    if(i==5)
      next
    else 
      puts i
    end
  end
end

obj=TestBreak.new()


输入号码
10

1
2
3
4
6
7
8
9
10

Use next, it will bypass that condition and rest of the code will work.
Below i have provided the Full script and out put

class TestBreak
  puts " Enter the nmber"
  no= gets.to_i
  for i in 1..no
    if(i==5)
      next
    else 
      puts i
    end
  end
end

obj=TestBreak.new()

Output:
Enter the nmber
10

1
2
3
4
6
7
8
9
10

别忘他 2024-10-06 14:22:29

后期添加:您可能根本不想使用 next,而是拒绝任何小于 2 的值,然后打印其余的值。

通过使用#lazy,我们可以避免在.reject { |x| 处创建中间数组。 x < 2 } 阶段。对于如此小的样本量来说意义不大,但如果我们在 (0..10_000_000) 上运行它,就会更有意义。

(0..5).lazy.reject { |x|
    x < 2
}.each { |x|
    puts x
}

Late addition: you may not want to use next at all, but rather reject any values less than 2 and then print the rest.

By using #lazy we avoid the creation of an intermediate array at the .reject { |x| x < 2 } stage. Not very meaningful for such a small sample size, but if we ran this on (0..10_000_000) for instance, it would be much more meaningful.

(0..5).lazy.reject { |x|
    x < 2
}.each { |x|
    puts x
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文