| SoftOver |
| Home Humor Puzzles Links Ruby News bLogs Books |
|
Try It
Ruby
Sometimes I need to make a call that may fail. I do not want to give up immediately, but do not want to try forever neither. In this case a small try function comes in handy:
# Make +count+ attempts to yield a block # ====Example: # def crazy_fun # puts a=rand # raise if a<0.9 # end # # try(5) {crazy_fun} rescue puts 'O-ops' def try(count) begin yield rescue retry if (count-=1)>0 raise end end |