Prośba o przekonwertowanie [PHP->Ruby]

Witam. Mógłby mi ktoś przekonwertować moduł Base64 (zmieniony, bo chce zmienić kolejność znaków) z języka PHP na Ruby?? Byłbym bardzo wdzięczny.

Source w PHP:

<?php


// Finded in the internet. Author is unknown.


class Base64 {


    /**

     * I have changed letter placement (P <=> x, S <=> 9) and the cases

     * You can completely redo the mapping table

     */


    private static $BinaryMap = array(

        'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', // 7

        'i', 'j', 'k', 'l', 'm', 'n', 'o', 'x', // 15

        'q', 'r', '9', 't', 'u', 'v', 'w', 'x', // 23

        'y', 'z', 'A', 'B', 'C', 'D', 'E', 'F', // 31

        'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', // 39

        'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', // 47

        'W', 'P', 'Y', 'Z', '0', '1', '2', '3', // 55

        '4', '5', '6', '7', '8', 'S', '+', '/', // 63

        '=', // padding char

    );


    public function __construct() {}


    public function base64_encode($input) {


        $output = "";

        $chr1 = $chr2 = $chr3 = $enc1 = $enc2 = $enc3 = $enc4 = null;

        $i = 0;


// $input = self::utf8_encode($input);


        while($i < strlen($input)) {

            $chr1 = ord($input[$i++]);

            $chr2 = ord($input[$i++]);

            $chr3 = ord($input[$i++]);


            $enc1 = $chr1 >> 2;

            $enc2 = (($chr1 & 3) << 4) | ($chr2 >> 4);

            $enc3 = (($chr2 & 15) << 2) | ($chr3 >> 6);

            $enc4 = $chr3 & 63;


            if (is_nan($chr2)) {

                $enc3 = $enc4 = 64;

            } else if (is_nan($chr3)) {

                $enc4 = 64;

            }


            $output .= self::$BinaryMap[$enc1]

                      . self::$BinaryMap[$enc2]

                      . self::$BinaryMap[$enc3]

                      . self::$BinaryMap[$enc4];

        }


        return $output;

    }


    public function utf8_encode($input) {

        $utftext = null;


        for ($n = 0; $n < strlen($input); $n++) {


            $c = ord($input[$n]);


            if ($c < 128) {

                $utftext .= chr($c);

            } else if (($c > 128) && ($c < 2048)) {

                $utftext .= chr(($c >> 6) | 192);

                $utftext .= chr(($c & 63) | 128);

            } else {

                $utftext .= chr(($c >> 12) | 224);

                $utftext .= chr((($c & 6) & 63) | 128);

                $utftext .= chr(($c & 63) | 128);

            }

        }


        return $utftext;

    }

}


?>

<?php

$string = pack('H*', "31c85c5aaa56c1f0102301ea497d0ab010e4e131af261787"); // HEX to binary

echo Base64::base64_encode($string);

echo "
";

echo base64_encode($string);

?>