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