# .max a Block of code with yield?

**URL:** <https://www.ruby-forum.com/t/max-a-block-of-code-with-yield/122529>\
**Category:** Ruby\
**Created:** [December 8, 2007, 9:03pm UTC](https://www.ruby-forum.com/t/max-a-block-of-code-with-yield/122529 "2007-12-08T21:03:06Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![wrishel](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/df705f/32.png) [@wrishel](https://www.ruby-forum.com/u/wrishel)\
**Post date:** [December 8, 2007, 9:03pm UTC](https://www.ruby-forum.com/t/max-a-block-of-code-with-yield/122529/1 "2007-12-08T21:03:06Z")

</div>

For a suitable value of pth, this works

require ‘find’  
Find.find (pth) { |x| puts x}

This doesn’t

require ‘find’  
Find.find (pth) { |x| yield(x)}.max

giving the error no block given (LocalJumpError), which I interpret as  
saying the yield doesn’t know to yield to max.

I am clearly trying to mix metaphors here.

There are obviously a lot of ways to compute the highest sortable path;  
I don’t need to know that.

As a learning experience I am just trying to clarify my understanding of  
Ruby.

It is possible that this is equivalent to asking if there is a way to  
feed the output of an iterator into somthing that is looking for the the  
.each iterator of an enum.

---

<div class="post-metadata">

**Author:** ![wrishel](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/df705f/32.png) [@wrishel](https://www.ruby-forum.com/u/wrishel)\
**Post date:** [December 8, 2007, 11:01pm UTC](https://www.ruby-forum.com/t/max-a-block-of-code-with-yield/122529/2 "2007-12-08T23:01:41Z")

</div>

Alle sabato 8 dicembre 2007, Wesley R. ha scritto:

> It is possible that this is equivalent to asking if there is a way to  
> feed the output of an iterator into somthing that is looking for the the  
> .each iterator of an enum.

The second piece of code raises an error because you can only use yield  
to  
call the block passed to the method. For instance:

def my\_method arg  
puts “this is my\_method”  
yield arg  
end

This will print the text “this is my\_method” on standard output, then  
call the  
block passed to the method, passing arg as argument to the block. It is  
analogous (there may be some subtle differences I’m not aware of) to the  
code

def my\_method arg, &blk  
puts “this is my\_method”  
blk.call arg  
end

Here the block is converted to a proc (the &blk argument), which is then  
called using its .call method.

If you use the first definition of my\_method without passing a block,  
you’ll  
get exactly an error like the one your second piece of code produces.

As far as I know, blocks can’t take blocks, so you can’t use yield in  
blocks.

If you want to use Enumerable#max on the files returned by Find.find, I  
think  
you can take one of these approaches:

1. you can create an array and fill it in the block you pass to find,  
then use  
its max method:  
files = []  
Find.find(pth){|x| files \<\< x}  
max\_value = files.max

2. Use use to\_enum to create an instance of a class which mixes in  
Enumerable  
from module Find and use its max method (note that, since Find.find  
requires  
a method, you need to pass a block to to\_enum) (see documentation for  
Enumerator.new and Object#to\_enum):  
require ‘enumerator’  
(Find.to\_enum(:find, pth’){|f| f}).max

I hope this helps

Stefano

---

<div class="post-metadata">

**Author:** ![wrishel](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/df705f/32.png) [@wrishel](https://www.ruby-forum.com/u/wrishel)\
**Post date:** [December 8, 2007, 11:04pm UTC](https://www.ruby-forum.com/t/max-a-block-of-code-with-yield/122529/3 "2007-12-08T23:04:02Z")

</div>

Wesley R. wrote:

> require ‘find’  
> Find.find (pth) { |x| yield(x)}.max
> 
> giving the error no block given (LocalJumpError), which I interpret as  
> saying the yield doesn’t know to yield to max.

yield(x) means: “Execute the block that has been given to this method  
with the  
parameter x”. You can’t yield to methods. Especially since the max won’t  
start  
executing before the find is finished executing.

> It is possible that this is equivalent to asking if there is a way to  
> feed the output of an iterator into somthing that is looking for the the  
> .each iterator of an enum.

That’s possible using to\_enum. To enum from the enumerator library  
(which  
is part of stdlib) can be called on any object and turns it into an enum  
by  
taking the method given as a parameter as an replacement for each. E.g.:

require ‘find’  
require ‘enumerator’  
Find.to\_enum(:find,pth).max

Or you could just use Dir[] instead of Find.find, which returns an array  
which  
is enumerable by default. Like Dir["/my/path/\*\*/\*"].max

HTH,  
Sebastian
