Tristin Davis
6/26/2008 12:46:00 AM
[Note: parts of this message were removed to make it a legal post.]
Thanks Ben. That worked perfect. No other changes required in the class. :)
Here's the final code. Maybe someone can get some use out of it. It sends a
heartbeat (of your choosing) from your running ruby app at a given interval.
class HeartBeat
attr_accessor :interval
def initialize(interval=120)
@interval = interval
@notifiers = Array.new
end
def add_notifier(notifier=nil, &block)
if block_given?
@notifiers << block
else
@notifiers << notifier
end
end
def thump
raise ArgumentError, "You must add_notifier before you can notify" if
@notifiers.empty?
@notifiers.each {|n| n.call }
end
def start
Thread.abort_on_exception = true
@thread = Thread.new do
while true
thump
sleep @interval
end
end
end
def stop
@thread.terminate
end
end
On Wed, Jun 25, 2008 at 7:05 PM, Ben Bleything <ben@bleything.net> wrote:
> On Thu, Jun 26, 2008, Tristin Davis wrote:
> > It just seems really unpretty to pass that nil when I want to pass just
> the
> > block.
>
> As an alternative to Ara's suggestion:
>
> def add_notifier( notifier = nil, &block )
>
> You'll need to change some of your internal logic as well to deal with
> notifier being nil sometimes, but I like this better than using splats.
> Personal taste.
>
> Ben
>
>