Skip to main content

migrating to php

Ruby • Asked by vampster

Hello,
I have inherited a ruby application and unfortunately have no knowledge of the language.
Could someone be so kind to help me figure out how to convert the below code to php?

  def generate_md5(*args)
    digest = Digest::MD5.new
    for arg in args do
      digest << arg.to_s
    end
    digest.hexdigest
  end

I have tried to get the same output in php, but I am failing.
I do not understand what happens in the for each loop.
The << seems to be equivalent to "update". What exactly does this update do?
Does it use the original hash, concatenate the new argument to it and hash the resulting string?
(this does not seem to generate the same output in php)
Or am I completely misunderstanding this (which is quite likely....)?


Hey vampster,

Does it use the original hash, concatenate the new argument to it and hash the resulting string?

In this particular case, yes. So for example if you have var = 'foo' and then var << 'bar' the value of var will now be 'foobar'

> digest = Digest::MD5.new
=> #<Digest::MD5: d41d8cd98f00b204e9800998ecf8427e>
> digest << "foo"
=> #<Digest::MD5: acbd18db4cc2f85cedef654fccc4a4d8>
> digest << "bar"
=> #<Digest::MD5: 3858f62230ac3c915f300c664312c63f>
> digest.hexdigest
=> "3858f62230ac3c915f300c664312c63f"
> var = 'foo'
=> "foo"
> var << 'bar'
=> "foobar"
> var
=> "foobar"

Check out https://medium.com/@AdamLombard/easy-ruby-plus-equals-vs-shovel-6f030875e366 if you want more details on the << function


Thanks for your help Jacob. I understand better now.


Login or Create An Account to join the conversation.

Subscribe to the newsletter

Join 24,647+ developers who get early access to new screencasts, articles, guides, updates, and more.

    By clicking this button, you agree to the GoRails Terms of Service and Privacy Policy.

    More of a social being? We're also on Twitter and YouTube.