String#digest

I don't think this already exists, but if it does I apologise for your inconvenience.

I propose that a method String#digest (or #checksum or whatever) is defined in the Digest class of the standard library.

   # Current
   digest = Digest::SHA1.new(str)

   # Proposed
   digest = str.digest(:sha1)

This can be implemented fairly easily:

   class String
     def digest (type = :md5)
       case type
       when :md5
         Digest::MD5.new(self)
       when :sha1
         Digest::SHA1.new(self)
       end
     end
   end

String#digest does not have to work like it does in my example. The key point here is that a message digest / hashsum should be a property of the string. I think it's the Ruby Way to Do It (TM).

Comments and constructive criticism are appreciated. Flames are not.

Cheers and remember Talk Like A Pirate Day the 19th,
Daniel

Daniel Schierbeck wrote:

I don't think this already exists, but if it does I apologise for your
inconvenience.

I propose that a method String#digest (or #checksum or whatever) is
defined in the Digest class of the standard library.

   # Current
   digest = Digest::SHA1.new(str)

   # Proposed
   digest = str.digest(:sha1)

This can be implemented fairly easily:

   class String
     def digest (type = :md5)
       case type
       when :md5
         Digest::MD5.new(self)
       when :sha1
         Digest::SHA1.new(self)
       end
     end
   end

String#digest does not have to work like it does in my example. The
key point here is that a message digest / hashsum should be a
property of the string. I think it's the Ruby Way to Do It (TM).

Hm, on one hand I agree to you as this seems natural. On the other hand
this introduces a dependency from String (a very basic class) to your
digest classes (not so basic). Also, you would have to maintain
String#digest every time there is a new algorithm unless you do it like

def digest(algo) algo.new(self) end

"foo".digest(Digest::SHA1)

But something tells me it's not worth the effort...

Cheers and remember Talk Like A Pirate Day the 19th,

What's that? Do pirates actually talk?

Kind regards

    robert

Robert Klemme wrote:

Hm, on one hand I agree to you as this seems natural. On the other hand
this introduces a dependency from String (a very basic class) to your
digest classes (not so basic).

That's why I think String#digest should be defined by the Digest class/module. You will still have to do a

   require "digest"

> Also, you would have to maintain String#digest

I don't think that's a problem; it's the same now, you just have to instantiate the classes yourself. If another algorithm is added to the Digest module it would be easy to add it to #digest as well.

>>Cheers and remember Talk Like A Pirate Day the 19th,
> What's that? Do pirates actually talk?

Arrr, ye landlubber! We be talkin' all day! Yaargh!

Ahoy mateys!
Daniel "Swashbuckler" Schierbeck :wink:

Daniel Schierbeck wrote:

Robert Klemme wrote:

Hm, on one hand I agree to you as this seems natural. On the other
hand this introduces a dependency from String (a very basic class)
to your digest classes (not so basic).

That's why I think String#digest should be defined by the Digest
class/module. You will still have to do a

   require "digest"

> Also, you would have to maintain String#digest

I don't think that's a problem; it's the same now, you just have to
instantiate the classes yourself. If another algorithm is added to the
Digest module it would be easy to add it to #digest as well.

Yepp, right. For I moment I forgot Ruby's unique openness and
extensibility. Shame on me!

>>Cheers and remember Talk Like A Pirate Day the 19th,
> What's that? Do pirates actually talk?

Arrr, ye landlubber! We be talkin' all day! Yaargh!

Ahoy mateys!
Daniel "Swashbuckler" Schierbeck :wink:

*GULP*

    robert