Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > Using yield

Reply
Thread Tools

Using yield

 
 
Joe Van Dyk
Guest
Posts: n/a
 
      12-04-2004
I come from a heavy C++ background, discovered Ruby a few months ago and
love it.

I've found that using blocks is a very natural thing. However, I have not
once used 'yield'. I'm sure that there are events when using yield would be
helpful, but I have no clue when it would be appropriate to use.

Thoughts? When do you use the 'yield' statement in code?

Joe


 
Reply With Quote
 
 
 
 
Sam Stephenson
Guest
Posts: n/a
 
      12-04-2004
On Sat, 4 Dec 2004 09:32:39 +0900, Joe Van Dyk <(E-Mail Removed)> wrote:
> When do you use the 'yield' statement in code?


When you want to write methods that take blocks.

| def repeatedly_write(times, &block)
| times.times do |i|
| puts yield(i)
| end
| end
|
| repeatedly_write(5) do |i|
| "iteration #{i}"
| end

prints:
iteration 0
iteration 1
iteration 2
iteration 3
iteration 4

In this example, ``yield(i)'' and ``block.call(i)'' are equivalent.

> Joe


Sam


 
Reply With Quote
 
 
 
 
Tim Hunter
Guest
Posts: n/a
 
      12-04-2004
Joe Van Dyk wrote:

> I come from a heavy C++ background, discovered Ruby a few months ago and
> love it.
>
> I've found that using blocks is a very natural thing. However, I have not
> once used 'yield'. I'm sure that there are events when using yield would
> be helpful, but I have no clue when it would be appropriate to use.
>
> Thoughts? When do you use the 'yield' statement in code?
>
> Joe


I use it with methods that create resources that need to be cleaned up
after, the way File.open closes the file for you after the block
terminates. If you're coming from C++, think destructors.

Here's an example from RMagick. The Image#view method extracts a rectangle
of pixels from an image and yields to a block (if present). Within the
block you can address the pixels using [i][j] indexes. When the block ends,
if any of the pixels have been modified all the pixels are stored
("sync'd") back to the image. The code looks like this:

def view(x, y, width, height)
view = View.new(self, x, y, width, height)
if block_given?
begin
yield(view)
ensure
view.sync
end
return nil
else
return view
end
end

You use #view like this:

image.view(5, 10, 20, 20) do |view|
view[5][7] = 'yellow'
# other useful stuff...
end



 
Reply With Quote
 
David A. Black
Guest
Posts: n/a
 
      12-04-2004
Hi --

On Sat, 4 Dec 2004, Sam Stephenson wrote:

> On Sat, 4 Dec 2004 09:32:39 +0900, Joe Van Dyk <(E-Mail Removed)> wrote:
> > When do you use the 'yield' statement in code?

>
> When you want to write methods that take blocks.
>
> | def repeatedly_write(times, &block)


You don't need &block there, since you don't use the block directly
(you just yield to it).


David

--
David A. Black
http://www.velocityreviews.com/forums/(E-Mail Removed)



 
Reply With Quote
 
David G. Andersen
Guest
Posts: n/a
 
      12-04-2004
On Sat, Dec 04, 2004 at 09:32:39AM +0900, Joe Van Dyk scribed:
> I come from a heavy C++ background, discovered Ruby a few months ago and
> love it.
>
> I've found that using blocks is a very natural thing. However, I have not
> once used 'yield'. I'm sure that there are events when using yield would be
> helpful, but I have no clue when it would be appropriate to use.
>
> Thoughts? When do you use the 'yield' statement in code?


I use it to pass in an additional filtering routine to
a database selection class. The selection thing knows some
basic stuff like time, but if you want to filter
more precisely,
you can pass it a block:

db_generator = new thingy(start_time, end_time) { |x|
x.attr1 == "frog" && x.attr2 >= 5 }


and then in later code you can grab stuff from the generator
and it's filtered in the way you specified.

-Dave
--
work: (E-Mail Removed) me: (E-Mail Removed)
MIT Laboratory for Computer Science http://www.angio.net/


 
Reply With Quote
 
Joe Van Dyk
Guest
Posts: n/a
 
      12-04-2004
Sam Stephenson wrote:
> On Sat, 4 Dec 2004 09:32:39 +0900, Joe Van Dyk
> <(E-Mail Removed)> wrote:
>> When do you use the 'yield' statement in code?

>
> When you want to write methods that take blocks.


Yes. What types of methods generally should take blocks?

Joe



 
Reply With Quote
 
gabriele renzi
Guest
Posts: n/a
 
      12-04-2004
Joe Van Dyk ha scritto:
> Sam Stephenson wrote:
>
>>On Sat, 4 Dec 2004 09:32:39 +0900, Joe Van Dyk
>><(E-Mail Removed)> wrote:
>>
>>>When do you use the 'yield' statement in code?

>>
>>When you want to write methods that take blocks.

>
>
> Yes. What types of methods generally should take blocks?
>


My little list:
[1] those when behaviour can be further specified.
i.e. #sort and #sort_by can get a block to specify how to sort an object

[2] those where a resource could be freed
i.e. constructors like File.new or Socket.new get a block so that the
undelying resource would be freed when the block ends instead of waiting
for the gc.

[3] those where one would like to lazyly use/reuse a snippet of code
given from the user

i.e. set_trace_func

There are surely others, the best way to get the felling is probably
looking at existing ruby code.
 
Reply With Quote
 
Brian Schröder
Guest
Posts: n/a
 
      12-04-2004
On Sat, 4 Dec 2004 09:32:39 +0900
"Joe Van Dyk" <(E-Mail Removed)> wrote:

> I come from a heavy C++ background, discovered Ruby a few months ago and
> love it.
>
> I've found that using blocks is a very natural thing. However, I have not
> once used 'yield'. I'm sure that there are events when using yield would be
> helpful, but I have no clue when it would be appropriate to use.
>
> Thoughts? When do you use the 'yield' statement in code?
>
> Joe


Yield is just a shortcut for block.call.

If I am Lazy, and I don't need to pass the block further I use yield. But I
believe that normally using block.call is more explicit and therefore
preferable.

It may be the case that block.call has a small performance penalty compared
with yield because the block needs to get bound to a variable and therefore be
turned into an object.

Regards,

Brian


--
Brian Schröder
http://ruby.brian-schroeder.de/quiz/crossword/ (Spoiler)



 
Reply With Quote
 
David A. Black
Guest
Posts: n/a
 
      12-04-2004
Hi --

On Sun, 5 Dec 2004, Brian [ISO-8859-15] Schröder wrote:

> On Sat, 4 Dec 2004 09:32:39 +0900
> "Joe Van Dyk" <(E-Mail Removed)> wrote:
>
> > I come from a heavy C++ background, discovered Ruby a few months ago and
> > love it.
> >
> > I've found that using blocks is a very natural thing. However, I have not
> > once used 'yield'. I'm sure that there are events when using yield would be
> > helpful, but I have no clue when it would be appropriate to use.
> >
> > Thoughts? When do you use the 'yield' statement in code?
> >
> > Joe

>
> Yield is just a shortcut for block.call.
>
> If I am Lazy, and I don't need to pass the block further I use yield. But I
> believe that normally using block.call is more explicit and therefore
> preferable.


'yield' is equally explicit, and equally unambiguous. It tells you
exactly as much as block.call does. I wouldn't worry about the
laziness part of it; if you measure laziness by power per word of
code, all Ruby programmers are already massively lazy


David

--
David A. Black
(E-Mail Removed)



 
Reply With Quote
 
Joel VanderWerf
Guest
Posts: n/a
 
      12-04-2004
Brian Schröder wrote:
> Yield is just a shortcut for block.call.


Yield has an advantage: rdoc can recognize it and generate docs for it,
including the names of the variables that are yielded to the block.

The speed advnatage of yield can also be significant, IMHO.


 
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
[ANN] To Yield or Not to Yield: An Inferable Question Michael Edgar Ruby 13 04-21-2011 05:55 PM
yield expression programmized-formal interpretation. (interpretationof yield expression.) castironpi@gmail.com Python 1 04-22-2008 01:32 AM
using Proc and yield Alber Eric Ruby 4 12-16-2006 03:30 AM
Yield & associations (was Proc / def /yield semantics) Markus Ruby 1 09-27-2004 07:04 PM
callbacks in ruby and using yield in resursion Paul Ruby 5 07-17-2003 01:19 PM



Advertisments