0

I'm trying to convert a Ruby script into a Python script (having absolutely no understanding of the various functions in Ruby) and can't find anything on the Ruby sub function.

Here's the function I'm trying to translate:

def getCCache(arg1, arg2, arg3)
    local4 = Digest::MD5.hexdigest(arg1 + arg2)
    local5 = 0
    while (local5 < arg3.length)
        temp1 = arg3[local5]
        temp2 = local5
        local5 += local5;
        local4.sub(temp1, arg3[temp2])
        local5 += 1
    end
    return (local4)
end

The line I'm having trouble with is local4.sub(temp1, arg3[temp2]). What does the sub function do? If there's an equivalent in Python I would appreciate that as well.

5
  • 2
    ruby-doc.org/core-2.1.0/String.html#method-i-sub - I'd have to say that's some horribly obscure Ruby you are converting. Variables called arg, temp and local - in fact it looks like it has been deliberately obfuscated? Commented Dec 29, 2013 at 21:12
  • I think it's less deliberately obfuscated and more poorly coded by the original programmer. Commented Dec 29, 2013 at 21:18
  • 3
    Is some sort of code generator involved in this or did a human actually write that code? Near as I can tell, that whole mess is just a long and confusing way to say Digest::MD5.hexdigest(arg1 + arg2). Commented Dec 29, 2013 at 21:38
  • 2
    I doubt you did a search for "ruby sub", which is the most obvious first step. Commented Dec 29, 2013 at 22:51
  • I did, all it brought up for me was references for the gsub method Commented Dec 30, 2013 at 6:23

2 Answers 2

2
local4.sub(temp1, arg3[temp2])

does nothing. It returns a copy of the string local4 with the first occurance of the substring referenced by temp1 substituted by the second argument. Then the result is discarded : no variable is assigned to the result.

local4 = local4.sub(temp1, arg3[temp2]) #or
local4.sub!(temp1, arg3[temp2])

would both do string substitution.

Sign up to request clarification or add additional context in comments.

1 Comment

Interesting. Now I'm really wondering what the hell's going on here. Thanks for answering my question.
0
sub(pattern) {|...| block } → $_
Equivalent to $_.sub(args), except that $_ will be updated if substitution occurs. Available only when -p/-n command line option specified.

$_ is a ruby global variable and it represents string last read by gets

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.