From: Stefano Crocco Date: 2007-12-09T06:57:53+09:00 Subject: Re: .max a Block of code with yield? 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