String#digest

D

Daniel Schierbeck

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
 
R

Robert Klemme

Daniel said:
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
 
D

Daniel Schierbeck

Robert said:
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.
What's that? Do pirates actually talk?

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


Ahoy mateys!
Daniel "Swashbuckler" Schierbeck ;)
 
R

Robert Klemme

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

require "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!
Arrr, ye landlubber! We be talkin' all day! Yaargh!


Ahoy mateys!
Daniel "Swashbuckler" Schierbeck ;)

*GULP*

robert
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,071
Latest member
MetabolicSolutionsKeto

Latest Threads

Top