我怎样才能提前从区块中返回一些东西?

发布于 2024-08-27 02:44:42 字数 233 浏览 9 评论 0原文

如果我想做这样的事情:

collection.each do |i|
   return nil if i == 3

   ..many lines of code here..
end

我怎样才能达到这样的效果?我知道我可以将块内的所有内容都包装在一个大的 if 语句中,但如果可能的话,我想避免嵌套。

Break 在这里不起作用,因为我不想停止其余元素的迭代。

If I wanted to do something like this:

collection.each do |i|
   return nil if i == 3

   ..many lines of code here..
end

How would I get that effect? I know I could just wrap everything inside the block in a big if statement, but I'd like to avoid the nesting if possible.

Break would not work here, because I do not want to stop iteration of the remaining elements.

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

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

发布评论

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

评论(4

笑叹一世浮沉 2024-09-03 02:44:42

块内的 next 从该块返回。块内的 break 从生成该块的函数返回。对于each,这意味着break 退出循环,而next 跳转到循环的下一次迭代(因此得名)。您可以使用下一个值中断值返回值。

next inside a block returns from the block. break inside a block returns from the function that yielded to the block. For each this means that break exits the loop and next jumps to the next iteration of the loop (thus the names). You can return values with next value and break value.

单调的奢华 2024-09-03 02:44:42
#!/usr/bin/ruby

collection = [1, 2, 3, 4, 5 ]

stopped_at = collection.each do |i|
   break i if i == 3

   puts "Processed #{i}"
end

puts "Stopped at and did not process #{stopped_at}"
#!/usr/bin/ruby

collection = [1, 2, 3, 4, 5 ]

stopped_at = collection.each do |i|
   break i if i == 3

   puts "Processed #{i}"
end

puts "Stopped at and did not process #{stopped_at}"
め可乐爱微笑 2024-09-03 02:44:42

在这种情况下,您可以使用break提前终止循环:

collection.each do |i|
  break if i == 3
  ...many lines
end

...当然,这是假设您实际上并不希望返回值,只是跳出块。

In this instance, you can use break to terminate the loop early:

collection.each do |i|
  break if i == 3
  ...many lines
end

...of course, this is assuming that you're not actually looking to return a value, just break out of the block.

〆凄凉。 2024-09-03 02:44:42

虽然这很古老,但有时仍然让我感到困惑。我需要这个来使用 [].select {|x| 来实现更复杂的用例}/[].reject {|x| }

常见用例

 [1,2,3,4,5].select{|x| x % 2 == 0 }
 => [2, 4] 

但我需要为每次迭代生成一个特定值并继续处理

具有更复杂的逻辑:

[1,2,3,4,5].select{|x| if x % 2 == 0; next true; end; false }
 => [2, 4] 
# you can also use `next(true)` if it's less confusing

此外,由于它与线程相关,因此此处使用 break 将发出您传入的单个值,如果条件命中:

[1,2,3,4,5].select{|x| if x % 2 == 0; break(true); end; false }
 => true 
[1,2,3,4,5].select{|x| if x % 2 == 0; break(false); end; false }
 => false 
[1,2,3,4,5].select{|x| if x % 2 == 0; break('foo'); end; false }
 => "foo" 

Although this is ancient, this still confuses me sometimes. I needed this for a more complicated use case with [].select {|x| }/[].reject {|x| }.

Common Use case

 [1,2,3,4,5].select{|x| x % 2 == 0 }
 => [2, 4] 

But I needed to yield a specific value for each iteration and continue processing

With more complicated logic:

[1,2,3,4,5].select{|x| if x % 2 == 0; next true; end; false }
 => [2, 4] 
# you can also use `next(true)` if it's less confusing

Also, since it's relevant to the thread, using break here will emit the single value you pass in if the conditional hits:

[1,2,3,4,5].select{|x| if x % 2 == 0; break(true); end; false }
 => true 
[1,2,3,4,5].select{|x| if x % 2 == 0; break(false); end; false }
 => false 
[1,2,3,4,5].select{|x| if x % 2 == 0; break('foo'); end; false }
 => "foo" 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文