新手问题 [Ruby] Ruby 中有没得什么 loop 是不需要调用 block 参数的?

hiveer · 2015年11月11日 · 最后由 shinkxw 回复于 2015年11月12日 · 1431 次阅读

" Like the return and break keywords, next may be used alone, or it may be followed by an expression or a comma-separated list of expressions. When next is used in a loop, any values following nextare ignored. In a block however the expression or expressions become the "return value" of the yield statement that invoked the block." (The Ruby Programming Language, David Flanagan & Yukihiro Matsumoto, 2008, page 150)

这里说的 next的第一种用法,也就是只用在 loop 中,而不是用在 block 中,什么时候会发生?

比如

for i in 1..10
  next if i % 2 == 0
  p i
end
需要 登录 后方可回复, 如果你还没有账号请 注册新账号