Skip to content

Instantly share code, notes, and snippets.

@gouf
Created November 26, 2013 23:02
Show Gist options
  • Save gouf/7667873 to your computer and use it in GitHub Desktop.
Save gouf/7667873 to your computer and use it in GitHub Desktop.
再帰処理で再試行してみる。データ取得は他の関数にやらせるといいかも。
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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment