.max a Block of code with yield?

W

Wesley Rishel

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

Stefano Crocco

Alle sabato 8 dicembre 2007, Wesley Rishel ha scritto:
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.

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
 
S

Sebastian Hungerecker

Wesley said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,968
Messages
2,570,153
Members
46,701
Latest member
XavierQ83

Latest Threads

Top