The Artima Developer Community
Sponsored Link

Ruby Buzz Forum
Method Check: String#sum

0 replies on 1 page.

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 0 replies on 1 page
Red Handed

Posts: 1158
Nickname: redhanded
Registered: Dec, 2004

Red Handed is a Ruby-focused group blog.
Method Check: String#sum Posted: Dec 22, 2004 12:21 PM
Reply to this message Reply

This post originated from an RSS feed registered with Ruby Buzz by Red Handed.
Original Post: Method Check: String#sum
Feed Title: RedHanded
Feed URL: http://redhanded.hobix.com/index.xml
Feed Description: sneaking Ruby through the system
Latest Ruby Buzz Posts
Latest Ruby Buzz Posts by Red Handed
Latest Posts From RedHanded

Advertisement
String#sum( n = 16 ) computes an n-bit checksum for a string. This amounts to addition of the character codes for a string. Proof: ?R + ?H == "RH".sum.

However, this isn’t much of a checksum considering that: ?S + ?G == "RH".sum. The clash between checksums is large.

A slightly better checksum is Object#hash, a method attached to every Ruby object. Since Object#hash uses a multiplier and bit shifter, its clash is diminished. While it is less predictable than String#sum, the hash from Object#hash can still be easily reversed, so don’t expect security from this hash.

As it stands, String#sum works for character math and Object#hash works for simplifying storage of Hash keys.

Note: Method Check is a column which investigates usage of a single method available in Ruby or a Ruby library. Ri conventions are used. The octothorpe symbol # is used to identify instance methods (such as Object#object_id), while the double-colon :: is used for class methods (such as Object::new.)

Read: Method Check: String#sum

Topic: Snapshots in Wee and Cyclic Weak References Previous Topic   Next Topic Topic: Now that’s cool…

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use