create a md5 hash from a string in ruby

To create an MD5 hash from a string in Ruby, you can use the Digest library that comes with Ruby's standard library:

main.rb
require 'digest'

string = "hello world"
md5_hash = Digest::MD5.hexdigest(string)

puts md5_hash # prints "5eb63bbbe01eeed093cb22bb8f5acdc3"
141 chars
7 lines

In the above code block, we require the digest library, which gives us access to a set of hash functions, including MD5.

We create a string variable with the value "hello world", and then call Digest::MD5.hexdigest on the string to generate an MD5 hash. The hexdigest method returns the hash as a string of hexadecimal digits.

We then print the resulting MD5 hash to the console.

gistlibby LogSnag