Posted By

shiftyjelly on 10/12/09


Tagged

ruby


Versions (?)

Who likes this?

1 person have marked this snippet as a favorite

fugufish


Ruby Tries Method


 / Published in: Ruby
 

  1. class Integer
  2. def tries options = {}, &block
  3. return if self < 1
  4. yield attempts ||= 1
  5. rescue options[:ignoring] || Exception
  6. retry if (attempts += 1) <= self
  7. end
  8. end
  9.  
  10. 3.tries { open 'http://vision-media.ca' }
  11.  
  12. 3.tries do |i|
  13. puts "Try ##{i} at opening..."
  14. open 'http://vision-media.ca'
  15. end
  16.  
  17. 3.tries(:ignoring => ParticularException) do
  18. open 'http://vision-media.ca'
  19. end

Report this snippet  

You need to login to post a comment.