Ruby:如何“下一步”外部循环?
file.each_line do |line|
#skip the first one/not a user
3.times { next } if first == 1
first = 2
end
如何获得“下一个”,“下一个”each_line 的迭代,而不是 3.times 迭代?另外,我怎样才能写这个看起来更好(即:first == 1看起来很糟糕)
file.each_line do |line|
#skip the first one/not a user
3.times { next } if first == 1
first = 2
end
How can I get the 'next' to well, "next" the iteration of the each_line, instead of the 3.times iteration? Also, how can I write this to look better (ie: first == 1 looks bad)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您可以使用
drop
方法来删除前几个元素:You can use
drop
method to, er, drop first couple of elements:您的内部循环可以在
break
之前设置一个标志变量(例如break_out = true
),并且您可以在退出内部循环后立即检查该变量。如果检测到该标志已设置,则跳出外循环。更有可能的是,有一种更好的方法来构建您的代码来完成您想要的事情。您只是想跳过前三行吗?如果是这样,请尝试以下操作:
Your inner loop can set a flag variable (say,
break_out = true
) before itbreak
s and you can check that variable as soon as you come out of the inner loop. If you detect the flag is set, break out of the outer loop.More likely, there is a better way of structuring your code to do what you want. Are you simply wanting to skip the first three lines? If so, try something like:
我认为你需要在其中添加另一个 if 语句
I think you'll need to add another if statement in there
如果文件不是太大,你可以这样做
If the file isn't too large, you can do