Ruby 从方法执行下一步
此代码:Ruby 从方法执行下一步,ruby,Ruby,此代码: def skip_if_three(count) puts 'three detected, let\'s skip this loop!' if count == 3 end 5.times do |count| skip_if_three(count) puts count end 返回: 0 1 2 three detected, let's skip this loop! 3
def skip_if_three(count)
puts 'three detected, let\'s skip this loop!' if count == 3
end
5.times do |count|
skip_if_three(count)
puts count
end
返回:
0
1
2
three detected, let's skip this loop!
3 # don't want this to appear!
4
但是,如果使用下一个关键字并执行以下操作:
def skip_if_three(count)
next if count == 3
end
5.times do |count|
skip_if_three(count)
puts count
end
我得到这个SyntaxerError:
下一个无效
这是意料之中的事。但是如何使用助手的next
更新
我使用嵌套循环,需要在每个循环中执行我的检查,所以我希望保持它干燥,因此使用外部方法
5.times do |i|
skip_if_three(i)
puts count
5.times do |j|
skip_if_three(j)
puts count
end
end
结果:
0
1
2
three detected, let's skip this loop!
4
0
1
2
three detected, let's skip this loop!
4
0
1
2
three detected, let's skip this loop!
4
结果:
0
1
2
three detected, let's skip this loop!
4
0
1
2
three detected, let's skip this loop!
4
0
1
2
three detected, let's skip this loop!
4
结果:
0
1
2
three detected, let's skip this loop!
4
0
1
2
three detected, let's skip this loop!
4
0
1
2
three detected, let's skip this loop!
4
结果:
0
1
2
three detected, let's skip this loop!
4
0
1
2
three detected, let's skip this loop!
4
0
1
2
three detected, let's skip this loop!
4
结果:
0
1
2
three detected, let's skip this loop!
4
0
1
2
three detected, let's skip this loop!
4
0
1
2
three detected, let's skip this loop!
4
结果:
0
1
2
three detected, let's skip this loop!
4
0
1
2
three detected, let's skip this loop!
4
0
1
2
three detected, let's skip this loop!
4
更好的解决方案是重新设计代码块,这样就不会出现这个问题。隐藏像next这样的功能并不理想,因此类似这样的东西会保留模型代码的简洁性,同时明确实际发生的情况:
def is_three? count
count == 3
end
5.times do |count|
next if is_three? count
puts count
end
更好的解决方案是重新设计代码块,这样就不会出现这个问题。隐藏像next这样的功能并不理想,因此类似这样的东西会保留模型代码的简洁性,同时明确实际发生的情况:
def is_three? count
count == 3
end
5.times do |count|
next if is_three? count
puts count
end
看起来不错,但我在实际代码中使用了嵌套循环,每个循环都有下一个功能。下一个if是三吗?计算每个嵌套循环的理想值?这是一个有趣的问题,我以前从未想过@sawa给出了一个有趣的答案,读者可能会发现这个答案在其他情况下很有用,也可能会提供其他创新的答案。是否有其他方法可以实现OP的最终目标?当然,你已经给出了一个答案,但那不是你所要求的。我同意有时建议其他方法是合适的,但是当问题像这个问题一样具体和新颖时,我认为答案应该局限于手头的问题。好的,接下来是一个关键字,你不能从方法调用堆栈中调用关键字,所以真正的答案是这是不可能的,但是试试。。。。下一步如果。。。更清楚地了解正在发生的事情。在我看来,抛接球似乎是鲁比的目标。六个月后,当我需要做一些代码考古学的时候,我会将清晰性置于创新之上。看起来不错,但我在实际代码中使用嵌套循环,每个循环都有下一个功能。下一个if是三吗?计算每个嵌套循环的理想值?这是一个有趣的问题,我以前从未想过@sawa给出了一个有趣的答案,读者可能会发现这个答案在其他情况下很有用,也可能会提供其他创新的答案。是否有其他方法可以实现OP的最终目标?当然,你已经给出了一个答案,但那不是你所要求的。我同意有时建议其他方法是合适的,但是当问题像这个问题一样具体和新颖时,我认为答案应该局限于手头的问题。好的,接下来是一个关键字,你不能从方法调用堆栈中调用关键字,所以真正的答案是这是不可能的,但是试试。。。。下一步如果。。。更清楚地了解正在发生的事情。在我看来,抛接球似乎是鲁比的目标。六个月后,当我需要做一些代码考古学时,我会选择清晰而不是创新。@Starkers这个答案是否有任何地方不符合您的要求?看起来不错,只是想知道是否有更好的方法来做。似乎我必须重复下一步,如果每个循环中有三个?计数。我只是想知道这是否有点枯燥,在每个循环中放置一个方法调用而不是一个操作符会更好一些,但是如果这是在没有一些反模式解决方法的情况下实现它的最好方法,那么我明白了。然后,您应该创建一个方法,该方法不仅在数字为3时跳过,而且在数字不为3时执行puts。@Starkers这个答案是否有任何不符合您要求的地方?似乎很好,只是想知道是否有更好的方法来实现它。似乎我必须重复下一步,如果每个循环中有三个?计数。我只是想知道这是否有点枯燥,在每个循环中放置一个方法调用而不是一个操作符会更好一些,但是如果这是在没有一些反模式解决方法的情况下实现它的最好方法,那么我明白了。然后,您应该创建一个方法,该方法不仅在数字为3时跳过,而且在数字不是3时执行puts。