2015-01-13 23 views
2

有沒有辦法在Opal中使用Ruby數學庫?Opal不能使用Ruby Math庫

我在我的ruby方法中使用Math::PI時收到以下錯誤消息Uncaught NameError: uninitialized constant Object::Math

紅寶石代碼:

class Numeric 
    def degrees 
    self * Math::PI/180 
    end 
end 

生成的JavaScript通過蛋白石:

/* Generated by Opal 0.6.3 */ 
(function($opal) { 
    var self = $opal.top, $scope = $opal, nil = $opal.nil, $breaker = $opal.breaker, $slice = $opal.slice, $klass = $opal.klass; 

    $opal.add_stubs(['$/', '$*']); 
    return (function($base, $super) { 
    function $Numeric(){}; 
    var self = $Numeric = $klass($base, $super, 'Numeric', $Numeric); 

    var def = self._proto, $scope = self._scope; 

    return (def.$degrees = function() { 
     var $a, $b, self = this; 

     return self['$*']((($a = ((($b = $scope.Math) == null ? $opal.cm('Math') : $b))._scope).PI == null ? $a.cm('PI') : $a.PI))['$/'](180); 
    }, nil) && 'degrees' 
    })(self, null) 
})(Opal); 

//# sourceMappingURL=/__opal_source_maps__/game_engine/numeric.js.map 
; 

感謝)

回答

2

Math模塊處於蛋白石的stdlib和不包括在所述默認的運行時(據我所知)。

根據您的部署環境,可能最容易將buildMath模塊(Opal::Builder.build('math'))放入文件中。

爲了您的具體的例子,不過,你可以只使用JS PI近似(這是所有歐泊的Math::PI確實是這樣):

class Numeric 
    def degrees 
    self * `Math.PI`/180 
    end 
end 
+0

太好了!由於我使用了很多數學函數,所以我將它構建成一個文件。謝謝 !! – jazzytomato

+3

如果有人需要將其構建到文件中,下面是從irb控制檯運行的代碼:'File.open(「math.js」,'w'){| file | file.write(Opal :: Builder.build('math'))} – jazzytomato