[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

Method Definitions: &block vs. yield

James Herdman

2/6/2006 10:37:00 PM

Suppose I had two methods:

# First method
def my_first_method(b)
yield b
end

# Second method
def my_second_method(&block)
block.call
end

I know that the block in the 2nd example is converted to a Proc object,
but a Proc object (to my newb eyes) doesn't seem to hold any advantages
that might make me want to use the latter method.

I was wondering if there are specific advantages to using the &block
convention or yield()ing a block? Is it merely a matter of preference
and or convention?

James

6 Answers

Ara.T.Howard

2/6/2006 11:01:00 PM

0

Joel VanderWerf

2/6/2006 11:03:00 PM

0

James H. wrote:
> Suppose I had two methods:
>
> # First method
> def my_first_method(b)
> yield b
> end
>
> # Second method
> def my_second_method(&block)
> block.call
> end

to be really comparable:

def my_second_method(b, &block)
block.call(b)
end

> I know that the block in the 2nd example is converted to a Proc object,
> but a Proc object (to my newb eyes) doesn't seem to hold any advantages
> that might make me want to use the latter method.
>
> I was wondering if there are specific advantages to using the &block
> convention or yield()ing a block? Is it merely a matter of preference
> and or convention?

Yield is lighter weight, since no Proc is created. Also, when a Proc is
created, it's binding is stored, and that binding includes any variables
in scope in the proc. As long as you hang on to that Proc, the values of
those variables cannot be GC-ed.

But with yield, you can only call the block from within the scope of the
method. If you need to store it away somewhere and call it after
returning, then you have to use a Proc.

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


Marcin Mielzynski

2/6/2006 11:09:00 PM

0

James H. wrote:

Generally yield is much faster than converting block to a Proc. But
there are situations when you have to explicitly pass the block and
convert it, these two are the most obvious that come to my mind:

saving a Proc object for delayed execution
implementing recursive iterators

lopex

Robert Klemme

2/7/2006 8:58:00 AM

0

Marcin MielżyÅ?ski wrote:
> James H. wrote:
>
> Generally yield is much faster than converting block to a Proc. But
> there are situations when you have to explicitly pass the block and
> convert it, these two are the most obvious that come to my mind:
>
> saving a Proc object for delayed execution
> implementing recursive iterators

More generally all situations where you do not want to invoke the block
from this method but pass it on to another.

Kind regards

robert

Jacob Fugal

2/7/2006 4:20:00 PM

0

On 2/7/06, Robert Klemme <bob.news@gmx.net> wrote:> Marcin Mielzynski wrote:> > James H. wrote:> >> > Generally yield is much faster than converting block to a Proc. But> > there are situations when you have to explicitly pass the block and> > convert it, these two are the most obvious that come to my mind:> >> > saving a Proc object for delayed execution> > implementing recursive iterators>> More generally all situations where you do not want to invoke the block> from this method but pass it on to another.(or both)Jacob Fugal

Gary Wright

2/9/2006 7:54:00 PM

0


On Feb 6, 2006, at 6:03 PM, Joel VanderWerf wrote:
> But with yield, you can only call the block from within the scope
> of the method. If you need to store it away somewhere and call it
> after returning, then you have to use a Proc.

You can pass an implicit block to another method by wrapping it in
another block:

class Example

def m1
m2 { yield }
end

def m2
yield
end

end

e1 = Example.new

e1.m1 { p 42 }

There is no explicit reification of a block to a Proc object
in this example. This technique can avoid the creation of
a Proc object in many common situations, but certainly not
in all cases.


Gary Wright