On Fri, Oct 15, 2004 at 12:03:17PM +0900, Nobuyoshi Nakada wrote:
> Hi,
> 
> At Fri, 15 Oct 2004 00:53:37 +0900,
> Michael Neumann wrote in [ruby-talk:116637]:
> > Implementation:
> > 
> >   # file: digest/from_io.rb
> >   class Digest::Base
> >     def self.from_io(io, block_size=8*1024)
> >       digest = new
> >       while data = io.read(block_size)
> >         digest.update(data)
> >       end
> >       digest
> >     end
> >   end
> 
> Another implementation could be:
> 
>   def Digest::Base.from(src)
>     digest = new
>     src.each(&digest.method(:update))
>     digest
>   end
> 
> This requires #each method instead of #read, do you think which
> is better?

What if #each does not return a string? Does #update work for all Ruby
objects? Personally I like #from_io more, as it's more natural how it
works. 

What if #from would take more arguments, like this:

  Digest.from(io, :each_chunk, blk_sz = 10000, bytes = 1_000_000)
  Digest.from(io, :each_line)

This would be a far more general solution, and as simple to implement.

> > Another addition would be the raw_digest method (which of course could
> > be better implemented in C):
> > 
> >   require 'enumerator'
> >   class Digest::Base
> >     def raw_digest
> >       hexdigest.to_enum(:scan, /../).map {|byte| byte.to_i(16).chr}.join
> >     end
> >     alias rawdigest raw_digest
> >   end
> 
> It is equivalent to Digest::Base#digest.

Oh, thanks.

Regards,

  Michael