Quick reference for efficient coding
This Ruby script utilizes the Digest::MD5
module to perform MD5 hashing operations. Here’s a breakdown of the code:
require 'digest/md5'
: This line includes the MD5 hashing functionality provided by the Digest
module in the Ruby standard library.
Digest::MD5.hexdigest('ruby')
: This line generates the MD5 hash for the string ‘ruby’ using the hexdigest
method. The resulting hash is a 32-character hexadecimal string representing the MD5 hash value of the input.
Digest::MD5.file('md5sum.rb').to_s
: This line calculates the MD5 hash of the contents of the file named ‘md5sum.rb’. It uses the file
method to read the file content and then applies the hexdigest
method to generate the MD5 hash. Finally, to_s
is called to convert the hash value into a string.
Note: The result of the first line is a string representing the MD5 hash of the ‘ruby’ string, and the result of the second line is the MD5 hash of the contents of the ‘md5sum.rb’ file, both expressed as strings. The script, as it stands, does not output or store these hash values for further use.
https://docs.ruby-lang.org/ja/latest/class/Digest=3a=3aBase.html
require 'digest/md5'
#=> true
Digest::MD5.hexdigest('ruby')
#=> "58e53d1324eef6265fdb97b08ed9aadf"
Digest::MD5.file('md5sum.rb').to_s
#=> "0e187136ae3345b14d4151ff227ef3e2"
Executed with Ruby 3.3.5