#!/usr/bin/ruby

# Translation of: https://rosettacode.org/wiki/Roman_numerals/Decode#Go

func roman2arabic(roman) {

    var arabic = 0;
    var last_digit = 1000;

    static m = :(
        I =>    1,
        V =>    5,
        X =>   10,
        L =>   50,
        C =>  100,
        D =>  500,
        M => 1000,
    );

    roman.uc.split('').map{|n| m{n} \\ 0}.each { |digit|
        last_digit < digit && (
            arabic -= (2 * last_digit);
        );
        arabic += (last_digit = digit);
    };

    return arabic;
}

%w(MCMXC MMVIII MDCLXVI).each { |roman_digit|
    "%-10s == %d\n".printf(roman_digit, roman2arabic(roman_digit));
};
