Last active
December 15, 2015 22:50
-
-
Save eladmeidar/5336294 to your computer and use it in GitHub Desktop.
java hash code in ruby
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class String | |
def java_hash_code | |
size = self.size | |
hash = 0 | |
self.chars.each_with_index do |ch, i| | |
hash += 31 * ch.ord ** (size - (i + 1)) | |
end | |
[hash].pack("L") | |
end | |
end | |
puts 'Hello World'.java_hash_code |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This returns same result as java hashCode() does