Forum: Ruby Exit loop after checking for some time

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
5b728bc0ac9f4918bedef972ba3b896a?d=identicon&s=25 Claire Hill (clai2x)
on 2009-04-09 04:59
hi all, i'm really new to ruby and i need to do a script that will check
my email every 5minutes and after 20minutes of no new mail it will
produce "test fail" (im a tester by the way). i have found a cool code
for checking email every 5min but i could not find a way to stop it
after 20minutes? any help is greatly appreciated :)

here's the code: (i took credit to the originator of the code here and
my sincere thanks..:))

def time_block
  start_time = Time.now
  Thread.new { yield }
  Time.now - start_time
end

def repeat_every(seconds)
 begin
    time_spent = time_block { yield }
    sleep(seconds - time_spent)
 end while time_spent < seconds
end


repeat_every(300) { # checking of email ever 5 minutes
  puts "Start checking: #{Time.now}"
  # email checking here
  puts "End checking: #{Time.now}"
}
9770ee3431156709c728f6105dd98f0d?d=identicon&s=25 Bosko Ivanisevic (Guest)
on 2009-04-09 08:50
(Received via mailing list)
On Apr 9, 4:59 am, Claire Hill <cart...@gmail.com> wrote:
>   start_time = Time.now
>
> repeat_every(300) { # checking of email ever 5 minutes
>   puts "Start checking: #{Time.now}"
>   # email checking here
>   puts "End checking: #{Time.now}"}
>
> --
> Posted viahttp://www.ruby-forum.com/.

Easiest way is to add one more argument to the repeat_every function:

def repeat_every(seconds, checks_count)
 check_no = 0
 begin
    time_spent = time_block { yield }
    check_no += 1
    break if check_no >= checks_count
    sleep(seconds - time_spent)
 end while time_spent < seconds
end

And call it with:

repeat_every(300, 4) {....}
5b728bc0ac9f4918bedef972ba3b896a?d=identicon&s=25 Claire Hill (clai2x)
on 2009-04-09 09:09
Bosko Ivanisevic wrote:

> Easiest way is to add one more argument to the repeat_every function:
>
> def repeat_every(seconds, checks_count)
>  check_no = 0
>  begin
>     time_spent = time_block { yield }
>     check_no += 1
>     break if check_no >= checks_count
>     sleep(seconds - time_spent)
>  end while time_spent < seconds
> end
>
> And call it with:
>
> repeat_every(300, 4) {....}

hey there, thank you so much..you save me!!! my heartfelt thanks :)
E16e84e861c1815ce11ba7bd851c857d?d=identicon&s=25 lasitha (Guest)
on 2009-04-09 15:38
(Received via mailing list)
On Thu, Apr 9, 2009 at 8:29 AM, Claire Hill <cartajo@gmail.com> wrote:
> [...]
>
> def repeat_every(seconds)
>  begin
>    time_spent = time_block { yield }
>    sleep(seconds - time_spent)
>  end while time_spent < seconds
> end

FYI, if time_spent > seconds in the above method, the call to sleep
will raise 'ArgumentError: time interval must be positive'.

cheers,
lasitha
5b728bc0ac9f4918bedef972ba3b896a?d=identicon&s=25 Claire Hill (clai2x)
on 2009-04-13 03:44
lasitha wrote:
> On Thu, Apr 9, 2009 at 8:29 AM, Claire Hill <cartajo@gmail.com> wrote:
>> [...]
>>
>> def repeat_every(seconds)
>> �begin
>> � �time_spent = time_block { yield }
>> � �sleep(seconds - time_spent)
>> �end while time_spent < seconds
>> end
>
> FYI, if time_spent > seconds in the above method, the call to sleep
> will raise 'ArgumentError: time interval must be positive'.
>
> cheers,
> lasitha

hi lasitha..:) thanks for the info...sure it helps a lot..:)
This topic is locked and can not be replied to.