[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

A cleaner way to pass a block or proc

Tristin Davis

6/25/2008 11:48:00 PM

[Note: parts of this message were removed to make it a legal post.]

Is there a cleaner way to implement my add_notifier method?

def add_notifier(notifier, &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


Here's how the method will be used.

hb.add_notifier nil do
puts "Notification1"
end

meth = lambda{ puts "notification2" }
hb.add_notifier(meth)

It just seems really unpretty to pass that nil when I want to pass just the
block.

14 Answers

ara.t.howard

6/25/2008 11:52:00 PM

0


On Jun 25, 2008, at 5:48 PM, Tristin Davis wrote:

> def add_notifier(notifier, &block)
> if block_given?
> @notifiers << block
> else
> @notifiers << notifier
> end
> end


def add_notifier *argv, &block
argv.push block
@notifiers.push *argv
end


a @ http://codeforp...
--
we can deny everything, except that we have the possibility of being
better. simply reflect on that.
h.h. the 14th dalai lama




Ben Bleything

6/26/2008 12:06:00 AM

0

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

Tristin Davis

6/26/2008 12:46:00 AM

0

[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
>
>

Joel VanderWerf

6/26/2008 1:05:00 AM

0

Tristin Davis wrote:
> def add_notifier(notifier=nil, &block)
> if block_given?
> @notifiers << block
> else
> @notifiers << notifier
> end
> end

def add_notifier(notifier=nil, &block)
@notifiers << (block || notifier)
end

--
vjoel : Joel VanderWerf : path berkeley edu : 510 665 3407

Jesús Gabriel y Galán

6/26/2008 9:30:00 AM

0

On Thu, Jun 26, 2008 at 3:04 AM, Joel VanderWerf
<vjoel@path.berkeley.edu> wrote:
> Tristin Davis wrote:
>>
>> def add_notifier(notifier=nil, &block)
>> if block_given?
>> @notifiers << block
>> else
>> @notifiers << notifier
>> end
>> end
>
> def add_notifier(notifier=nil, &block)
> @notifiers << (block || notifier)
> end

I think you could end up adding some nils with
this solution and the previous one, if I call:

Heartbeat.new.add_notifier

without any argument or block. Maybe you will
want to check it in the add_notifier method, or
at least handle it in the thump method, to avoid
calling the call method on nil. Maybe something like:

def add_notifier(notifier=nil, &block)
raise "Both nil" unless (notifier || block)
@notifiers << (block || notifier)
end

Jesus.

Robert Dober

6/26/2008 10:40:00 AM

0

On Thu, Jun 26, 2008 at 3:04 AM, Joel VanderWerf
<vjoel@path.berkeley.edu> wrote:
> Tristin Davis wrote:
>>
>> def add_notifier(notifier=3Dnil, &block)
>> if block_given?
>> @notifiers << block
>> else
>> @notifiers << notifier
>> end
>> end
>
> def add_notifier(notifier=3Dnil, &block)
> @notifiers << (block || notifier)
> end

I would do it exactly as J=F6el suggested above, however there is an
interesting alternative one might want to be aware of

def add_notfier notifier=3Dnil
@notifiers << ( Proc::new rescue notifier)
end

Cheers
Robert


--=20
http://ruby-smalltalk.blo...

---
Les m=EAmes questions qu'on se pose
On part vers o=F9 et vers qui
Et comme indice pas grand-chose
Des roses et des orties.
-
Francis Cabrel

ara.t.howard

6/26/2008 2:27:00 PM

0


On Jun 26, 2008, at 4:39 AM, Robert Dober wrote:

> def add_notfier notifier=nil
> @notifiers << ( Proc::new rescue notifier)
> end

still adds nil if no args given though...


cheers.

a @ http://codeforp...
--
we can deny everything, except that we have the possibility of being
better. simply reflect on that.
h.h. the 14th dalai lama




Ben Bleything

6/26/2008 3:20:00 PM

0

On Thu, Jun 26, 2008, Jess Gabriel y Galn wrote:
> I think you could end up adding some nils with
> this solution and the previous one, if I call:
>
> Heartbeat.new.add_notifier
>
> without any argument or block.

Right, this is why I suggested modifying the block to handle the nils :)

Ben

Tristin Davis

6/26/2008 5:50:00 PM

0

[Note: parts of this message were removed to make it a legal post.]

The final result.

def add_notifier(notifier=nil, &block)
raise ArgumentError, "Nil notifier. You must provide a Proc or a
block" if notifier.nil? && block.nil?
@notifiers << (notifier || block)
end

On Thu, Jun 26, 2008 at 10:19 AM, Ben Bleything <ben@bleything.net> wrote:

> On Thu, Jun 26, 2008, Jess Gabriel y Galn wrote:
> > I think you could end up adding some nils with
> > this solution and the previous one, if I call:
> >
> > Heartbeat.new.add_notifier
> >
> > without any argument or block.
>
> Right, this is why I suggested modifying the block to handle the nils :)
>
> Ben
>
>

Robert Dober

6/26/2008 6:45:00 PM

0

On Thu, Jun 26, 2008 at 4:27 PM, ara.t.howard <ara.t.howard@gmail.com> wrote:
>
> On Jun 26, 2008, at 4:39 AM, Robert Dober wrote:
>
>> def add_notfier notifier=nil
>> @notifiers << ( Proc::new rescue notifier)
>> end
>
> still adds nil if no args given though...
>
>
> cheers.
>
> a @ http://codeforp...
> --
> we can deny everything, except that we have the possibility of being better.
> simply reflect on that.
> h.h. the 14th dalai lama
>
That is what OP wanted IIANM, the nils raise an exception later on.
Anyway I thought I had made it clear that your code is preferable but
I feel that teaching what Proc::new is about, is part of our job here
;)

Cheers
Robert