Converting some Ruby (Digest) .. scant documentation

Preface by saying by no stretch do I pretend to be a programmer, just make short scripts and utils for personal use.

Only recently delved into Ruby and made a couple of maintenance scripts, but I can’t seem to wrap starkits like I can with Python or TCL, so was excited to find Crystal. I found one roadblock in a script I was converting; the script calls this block many times during its run:

def do_file_digest(file_in)
	return (Digest::SHA512.file file_in).to_s

There seems to be a file method in Crystal as far as I can tell, but I can’t seem to implement it correctly.

For what it’s worth, the other call to digest in my Ruby script hashes strings:

var = Digest::SHA512.hexdigest <string>

and it works fine in Crystal. Hopefully someone can point me in the right direction with regard to implementing .file, thanks.

You can use the block version of .hexdigest, then call the #file method. E.g.

def do_file_digest(file_path : String | Path) : String
  Digest::SHA512.hexdigest &.file file_path

It’s a bit not so clear, but the block version yields a Digest - Crystal 1.11.2 instance which implements it, but the yielding variant of the class method is defined in Digest::ClassMethods - Crystal 1.11.2 since it’s used in each digest type.

Did the trick, thanks much