Skip to content

Instantly share code, notes, and snippets.

@hoshinotsuyoshi
Created November 25, 2012 13:28
Show Gist options
  • Save hoshinotsuyoshi/4143485 to your computer and use it in GitHub Desktop.
Save hoshinotsuyoshi/4143485 to your computer and use it in GitHub Desktop.
エラーが出るかもしれない任意のメソッドを最大n回試行するメソッド
def commit(symbol,*arg)
c = 0
begin
return self.send(symbol.to_sym,*arg)
rescue
c += 1
retry if c < 5
end
"get less than 90 5 times..."
end
def try_to_point_get_more_than_90
point = rand * 100
raise Error if point < 90
point
end
commit :try_to_point_get_more_than_90
#=> 90.01797302249399
commit :try_to_point_get_more_than_90
#=> "get less than 90 5 times..."
commit :try_to_point_get_more_than_90
#=> "get less than 90 5 times..."
commit :try_to_point_get_more_than_90
#=> "get less than 90 5 times..."
commit :try_to_point_get_more_than_90
#=> "get less than 90 5 times..."
commit :try_to_point_get_more_than_90
#=> 90.45187243345141
commit :try_to_point_get_more_than_90
#=> "get less than 90 5 times..."
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment