Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > passing block to another function

Reply
Thread Tools

passing block to another function

 
 
Shea Martin
Guest
Posts: n/a
 
      03-20-2006
I have a function:

def worker( &p_block )
....
p_block.call( my_data )
....
end

I have a wrapper function, that I would like to use to call 'worker'.
But I would like the caller of 'wrapper' to be able to specify the block
'wrapper' will use. i.e.,

def wrapper( &p_block )
....
worker{ p_block }
end

I suspect that this is possible, but I can't seem to nail the syntax.

?

~S
 
Reply With Quote
 
 
 
 
Shea Martin
Guest
Posts: n/a
 
      03-20-2006
Shea Martin wrote:

> I suspect that this is possible, but I can't seem to nail the syntax.


Ok,

The following works, is this the
def worker( p_num, &p_block )
for i in 0...p_num
p_block.call( i )
end
end

def five_times( &p_block )
worker( 5 ){ |x| p_block.call(x) }
end

five_times { |i| puts "#{i}. hello" }


..

~S
 
Reply With Quote
 
 
 
 
Marcel Molina Jr.
Guest
Posts: n/a
 
      03-20-2006
On Tue, Mar 21, 2006 at 04:48:50AM +0900, Shea Martin wrote:
> I have a function:
>
> def worker( &p_block )
> ....
> p_block.call( my_data )
> ....
> end
>
> I have a wrapper function, that I would like to use to call 'worker'.
> But I would like the caller of 'wrapper' to be able to specify the block
> 'wrapper' will use. i.e.,
>
> def wrapper( &p_block )
> ....
> worker{ p_block }
> end
>
> I suspect that this is possible, but I can't seem to nail the syntax.


def worker(&block)
block.call(data)
end

def wrapper(&block)
worker(&block)
end

marcel
--
Marcel Molina Jr. <>


 
Reply With Quote
 
Joel VanderWerf
Guest
Posts: n/a
 
      03-20-2006
Shea Martin wrote:
> I have a function:
>
> def worker( &p_block )
> ....
> p_block.call( my_data )
> ....
> end
>
> I have a wrapper function, that I would like to use to call 'worker'.
> But I would like the caller of 'wrapper' to be able to specify the block
> 'wrapper' will use. i.e.,
>
> def wrapper( &p_block )
> ....
> worker{ p_block }
> end
>
> I suspect that this is possible, but I can't seem to nail the syntax.


def wrapper
worker {yield}
end

It makes sense once you "wrap" your head around it

(Also, you might want, if possible, to use yield in worker, as well.
It's more efficient.)

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


 
Reply With Quote
 
Shea Martin
Guest
Posts: n/a
 
      03-20-2006

> It makes sense once you "wrap" your head around it
>
> (Also, you might want, if possible, to use yield in worker, as well.
> It's more efficient.)
>


Yeah, it seems dead obvious *now*. That is good to know about 'yield'
being more efficient. I always used the named Proc, as I prefer it
syntactically (and I always mispell yeild ). I will start using yield.

~S
 
Reply With Quote
 
Joel VanderWerf
Guest
Posts: n/a
 
      03-20-2006
Shea Martin wrote:
>
>> It makes sense once you "wrap" your head around it
>>
>> (Also, you might want, if possible, to use yield in worker, as well.
>> It's more efficient.)
>>

>
> Yeah, it seems dead obvious *now*. That is good to know about 'yield'
> being more efficient. I always used the named Proc, as I prefer it
> syntactically (and I always mispell yeild ). I will start using yield.
>
> ~S


Just found my benchmark to back this assertion up:

$ cat yield-vs-proc.rb
require 'benchmark'

def outer11(&bl)
inner1(&bl)
end

def outer12(&bl)
inner2(&bl)
end

def outer21
inner1 {yield}
end

def outer22
inner2 {yield}
end

def inner1(&bl)
bl.call
end

def inner2
yield
end

n = 100000

Benchmark.bmbm(10) do |rpt|
rpt.report("outer11") do
n.times {outer11{}}
end

rpt.report("outer12") do
n.times {outer12{}}
end

rpt.report("outer21") do
n.times {outer21{}}
end

rpt.report("outer22") do
n.times {outer22{}}
end
end

__END__

Output:

Rehearsal ---------------------------------------------
outer11 0.890000 0.010000 0.900000 ( 0.894500)
outer12 0.370000 0.000000 0.370000 ( 0.364880)
outer21 0.770000 0.000000 0.770000 ( 0.77663
outer22 0.170000 0.000000 0.170000 ( 0.163393)
------------------------------------ total: 2.210000sec

user system total real
outer11 0.490000 0.000000 0.490000 ( 0.491969)
outer12 0.400000 0.000000 0.400000 ( 0.396264)
outer21 0.760000 0.000000 0.760000 ( 0.76450
outer22 0.160000 0.000000 0.160000 ( 0.161982)

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


 
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
Passing a block to a block Brian Candler Ruby 2 11-04-2008 08:07 PM
Fo:Block can you check to see if a block contains any text by using the block id? morrell XML 1 10-10-2006 07:18 PM
yielding a block to another block David Chelimsky Ruby 4 09-02-2006 11:37 PM
XML schema validation of one xml block based on values from another xml block Andy XML 0 11-18-2004 11:04 PM



Advertisments