.max a Block of code with yield?

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.

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

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

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs