Ruby 关于 retry 和 redo 的问题请教

shallow__pace · June 02, 2013 · Last by elson2010 replied at February 05, 2016 · 4220 hits

大家好,我的环境是 linux mint 15 & ruby 1.9.3

然后我编辑了下面这一段程序:

def repeat (condition)
  yield
  retry if not condition
end
j = 0
repeat (j >= 10) {
  j += 1
  puts j
}
repeat

但是编译器报错了:

/home/sergio/Dropbox/codeworkspace/ruby_practice/custom_iterator.rb:3: Invalid retry
/home/sergio/Dropbox/codeworkspace/ruby_practice/custom_iterator.rb: compile error (SyntaxError)

我试了下将retry替换成redo,但是还是报错 Invalid redo。。。

各位大大们知道是怎么一回事吗?

def repeat
  yield
end
j = 0
repeat do
  j += 1
  puts j
  redo if j < 10
end

这跟 retry 没关系

#1 楼 @Eagle retry 的方式如何实现呢?这是「the ruby way」里面的一段源码,我还是没理解 retry 该怎么使用

我懂你意思了 你看的用法只能在 1.8 里面用,1.9 不支持 retry 用来 重新开始循环 了

#3 楼 @Eagle 噢,明白了,谢谢~~

retry 貌似只能在 rescue 块里用

不知道哪里有比较新的 ruby 教程呢

You need to Sign in before reply, if you don't have an account, please Sign up first.