Si te parece que debería heredar, que herede... no lo puse por que en
principio pensaba usar el method_missing... después no lo necesité...

On 8/31/07, Damian Janowski <[EMAIL PROTECTED]> wrote:
>
> 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
>
> Muy bueno Nacho!
>
> Una pregunta, ¿por alguna razón la clase no hereda de Dir?
>
> Igual se le puede agregar el method_missing para redirigir todos los
> mensajes a @dir.
>
> Gracias por compartirlo ;)
> _______________________________________________
> Ruby mailing list
> [email protected]
> http://lista.rubyargentina.com.ar/listinfo.cgi/ruby-rubyargentina.com.ar
>
_______________________________________________
Ruby mailing list
[email protected]
http://lista.rubyargentina.com.ar/listinfo.cgi/ruby-rubyargentina.com.ar

Responder a