Skip to content

Maintenance Ruby 3.4 gets fixes, but no new features. Switch to Ruby 4.0 →

instance method

String#sum

(?::int bits) -> ::Integer
sum(n = 16) → integer

Returns a basic n-bit checksum of the characters in self; the checksum is the sum of the binary value of each byte in self, modulo 2**n - 1:

Example
'hello'.sum     # => 532
'hello'.sum(4)  # => 4
'hello'.sum(64) # => 532
'тест'.sum      # => 1405
'こんにちは'.sum  # => 2582

This is not a particularly strong checksum.

See also #succ! #swapcase #succ #swapcase! also in 4.1 · 4.0 · 3.3 · 3.2 · 3.1 · 3.0