On 8/31/07, NachoKB <[EMAIL PROTECTED]> wrote:
> Cuidado que Find no es una clase, sino un módulo.
>
> O sea, no es para usarlo directamente... normalmente es para incorporarlo en
> una clase (para componer comportamiento). Lo mismo con FileTest.
>
> Me parece que lo que querés hacer lo podés hacer sólo usando File y Dir...
>
> Básicamente, vos querés agarrar todos los archivos a partir de un root dado
> (recursivamente) y calcularle el MD5:
>
> > Dir.new(directorio).map do |fullpath|
> >   [name, Digest::MD5.hexdigest (File.read(fullpath))]
> > end
> >
>
> El problema es que (1) si bien Dir es Enumerable, enumera sólo los hijos
> directo (no rescursivamente) y (2) "fullpath" no quedaría realmente el path
> completo... Por lo tanto hice esto, ver qué opinan:
>
> > class TraversableDir
> >   include Enumerable
> >   def initialize(*args, &block)
> >     @dir = Dir.new *args, &block
> >   end
> >   def each(&block)
> >     @dir.each do |name|
> >       unless ['.', '..'].include? name
> >         fullpath = self.fullpath(name)
> >         case
> >           when File.directory?(fullpath)
> >             TraversableDir.new(fullpath).each &block
> >           when File.file?(fullpath)
> >             block.call fullpath
> >           else
> >             p "Ojo ni dir ni file: #{fullpath} (hay que hacer algo?)"
> >         end
> >       end
> >     end
> >   end
> >   def fullpath(name)
> >     File.expand_path "[EMAIL PROTECTED]/#{name}"
> >   end
> > end
> >
>
> Entonces queda:
> > TraversableDir.new(directorio).map do |fullpath|
> >   [name, Digest::MD5.hexdigest (File.read(fullpath))]
> > end
> >
>
> Lo que te daria un array de esta forma:
> > [
> >   [ "/path/a1", "ABDCE" ],
> >   [ "/path/a2", "DEADBEEF" ]
> > ]
> >
>
> Que es un idiom usado en Ruby (es similar a un Hash pero a veces se usa así,
> por ejemplo cuando no necesitás acceder a un elemento, sino que lo vas a
> iterar).
>
>

Otra para no caer en el truco recursivo es usar Dir.glob o [] tambien,
indicando:

Dir.glob('/path/*') o '/path/**/*' para recursion.

(Lo mismo usando []):

Dir['/path/*']

Esto de entrega un array con todos los elementos "relativos" al path
que indicaste.

irb(main):030:0> Dir['*']
=> ["Pictures", "Documents", "Examples", "Templates", "Public",
"Music", "Desktop", "Videos"]

Saludos,

-- 
Luis Lavena
Multimedia systems
-
Leaders are made, they are not born. They are made by hard effort,
which is the price which all of us must pay to achieve any goal that
is worthwhile.
Vince Lombardi
_______________________________________________
Ruby mailing list
[email protected]
http://lista.rubyargentina.com.ar/listinfo.cgi/ruby-rubyargentina.com.ar

Responder a