Rubyループ(繰り返し)#3 - Silver/Gold試験対策
redoとnextの出力の違いを確認
ary = ("a".."d").to_a v = 0 ary.each do |str| v += 1 if v == 3 redo end print("#{v}: #{str}") unless str == ary.last print(", ") end end #=> 1: a, 2: b, 4: c, 5: d
next
ary = ("a".."d").to_a m = 0 ary.each do |str| m += 1 if m == 3 next end print("#{m}: #{str}") unless str == ary.last print(", ") end end #=> 1: a, 2: b, 4: d
retry/redoは区別すること。 redoは繰り返し節の中で、 retryはrescue節でbeginを繰り返す為に使用する
https://docs.ruby-lang.org/ja/latest/doc/spec=2fcontrol.html#retry