ruby 再帰处理で再试行してみる。データ取得は他の关数にやらせるといいかも。

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了ruby 再帰处理で再试行してみる。データ取得は他の关数にやらせるといいかも。相关的知识,希望对你有一定的参考价值。

a = [nil, nil, nil, 'hoge']
b = Array.new a #set same data

def get ar, rc
  return "Couldn't get data in retry count" if rc == 0
  puts "Trying get data..."; sleep 3
  dat = ar.shift
  return "Found!: \"#{dat}\"" unless dat.nil?
  puts 'data not found. retrying...'; sleep 1
  get ar, rc - 1
end

puts get a, 3
puts '--------------'
puts get b, 4

# Out put:
=begin
Trying get data...
data not found. retrying...
Trying get data...
data not found. retrying...
Trying get data...
data not found. retrying...
Couldn't get data in retry count
--------------
Trying get data...
data not found. retrying...
Trying get data...
data not found. retrying...
Trying get data...
data not found. retrying...
Trying get data...
Found!: "hoge"
=end

以上是关于ruby 再帰处理で再试行してみる。データ取得は他の关数にやらせるといいかも。的主要内容,如果未能解决你的问题,请参考以下文章