From: Austin Ziegler Date: 2004-10-16T23:14:10+09:00 Subject: Re: [RCR] digest/from_io On Sat, 16 Oct 2004 19:20:58 +0900, Michael Neumann wrote: > 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. #update should work on all Digest::Base subclasses. This line is roughly: src.each { |data| digest.update(data) } Digest#update methods should work with individual bytes or strings, shouldn't they? It also depends on what Digest#update does to the implied data value; if it calls #to_s or #to_str, then arrays of strings or other values could be dealt with very easily. To me, that would be as or more useful than limiting it to IO objects; I might want to generate a digest from the result of IO::readlines (an array of strings). -austin -- Austin Ziegler * halostatue@gmail.com * Alternate: austin@halostatue.ca : as of this email, I have [ 5 ] Gmail invitations