-4

i just try to complete this quiz but i can't understand what is doing that "amt" in "romanize" method:

ROMAN_NUMS = {
  "M" => 1000,
  "CM" => 900, "D" => 500, "CD" => 400, "C" => 100,
  "XC" => 90,  "L" => 50,  "XL" => 40,  "X" => 10,
  "IX" => 9,   "V" => 5,   "IV" => 4,   "I" => 1
}

def romanize(num)
  ROMAN_NUMS.map do |ltr, val| 
    amt, num = num.divmod(val)
    ltr * amt
  end.join
end
tadman
  • 208,517
  • 23
  • 234
  • 262
ELECON88
  • 11
  • 1
  • 4
  • [divmod](http://ruby-doc.org/core-1.9.3/Numeric.html#method-i-divmod) – zarak Aug 19 '16 at 04:13
  • 3
    When in doubt, search for method names like "Ruby divmod" and become enlightened. – tadman Aug 19 '16 at 04:14
  • http://ruby-doc.org/core-1.9.3/Numeric.html#method-i-divmod – HolyMoly Aug 19 '16 at 04:46
  • Thanks a lot!! i keep reading about "divmod" but i can't found some explain, i just notice it could be quotient derived from "divmod", but that becouse some good man tell me, however appreciate your recomendation. May i'll should ask "what try to describe 'amt' in method?" – ELECON88 Aug 19 '16 at 04:56
  • If any method `m` (such as `divmod`), when executed on a receiver `r`, returns an array `a` of size two, you can write `x, y = r.m` and `x` will be set equal to the first element of `a` and `y` will be set equal to the second element of `a`. This can become more complex. For example suppose `r.m` returns `[[1,2],3,4]`. You can then set (try it!) `(a,b),c,d = [[1,2],3,4]` and `a`, `b`, `c`, and `d` will respectively equal `1`, `2`, `3` and `4`. This combines "parallel assignment" with "disambiguation". – Cary Swoveland Aug 19 '16 at 05:19
  • Excellent explaination!! Thanks f...ing a lot!!! Really :) – ELECON88 Aug 19 '16 at 17:25

1 Answers1

1

divmod returns a 2-element array, made up of the quotient and the modulus.

So basically x.divmod(y) will return [x / y, x % y] (look at the docs for the more accurate description).

The line

amt, num = num.divmod(val)

takes that two element array and does a de-structuring assignment to two variables. Afterwards amt (which is just a badly named variable that should be called amount) will contain the first value of the returned array and num the second.

Michael Kohl
  • 66,324
  • 14
  • 138
  • 158