Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > A cleaner way to pass a block or proc

Reply
Thread Tools

A cleaner way to pass a block or proc

 
 
Tristin Davis
Guest
Posts: n/a
 
      06-25-2008
[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.

 
Reply With Quote
 
 
 
 
ara.t.howard
Guest
Posts: n/a
 
      06-25-2008

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://codeforpeople.com/
--
we can deny everything, except that we have the possibility of being
better. simply reflect on that.
h.h. the 14th dalai lama




 
Reply With Quote
 
 
 
 
Ben Bleything
Guest
Posts: n/a
 
      06-26-2008
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

 
Reply With Quote
 
Tristin Davis
Guest
Posts: n/a
 
      06-26-2008
[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 <> 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
>
>


 
Reply With Quote
 
Joel VanderWerf
Guest
Posts: n/a
 
      06-26-2008
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

 
Reply With Quote
 
Jesús Gabriel y Galán
Guest
Posts: n/a
 
      06-26-2008
On Thu, Jun 26, 2008 at 3:04 AM, Joel VanderWerf
<> 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.

 
Reply With Quote
 
Robert Dober
Guest
Posts: n/a
 
      06-26-2008
On Thu, Jun 26, 2008 at 3:04 AM, Joel VanderWerf
<> 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.blogspot.com/

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

 
Reply With Quote
 
ara.t.howard
Guest
Posts: n/a
 
      06-26-2008

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://codeforpeople.com/
--
we can deny everything, except that we have the possibility of being
better. simply reflect on that.
h.h. the 14th dalai lama




 
Reply With Quote
 
Ben Bleything
Guest
Posts: n/a
 
      06-26-2008
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

 
Reply With Quote
 
Tristin Davis
Guest
Posts: n/a
 
      06-26-2008
[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 <> 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
>
>


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
[Q] is it impossible to pass block into proc object? makoto kuwata Ruby 1 04-22-2012 12:33 PM
"break" from a block and a proc object used in block position Wolfgang Nádasi-Donner Ruby 0 05-31-2007 06:05 PM
proc A def/calls proc B: variable scoping rules. NevilleDNZ Python 9 08-16-2006 04:36 AM
Convert VB.NET to TSQL PROC & Reference a Proc from another Proc David Lozzi ASP .Net 3 06-01-2005 06:35 PM
What is the diff btwn 'sho proc' and 'sho proc cpu' William J King Cisco 1 12-18-2003 11:50 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57