gpt4 book ai didi

javascript - 不能将 Ruby Math 库与 Opal 一起使用

转载 作者:行者123 更新时间:2023-11-30 17:06:27 24 4
gpt4 key购买 nike

有没有办法在 Opal 中使用 ruby​​ 数学库?

在我的 ruby​​ 方法中使用 Math::PI 时,我收到以下错误消息 Uncaught NameError: uninitialized constant Object::Math

ruby 代码:

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

Opal 生成的 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
;

谢谢;)

最佳答案

Math 模块在 Opal 的 stdlib 中并且不包含在默认运行时中(据我所知)。

根据您的部署上下文,构建 Math 模块可能是最简单的方法 (Opal::Builder.build('math')) 到文件中。

不过,对于您的具体示例,您可以只使用 JS PI 近似值(这就是 Opal 的 Math::PI 所做的一切):

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

关于javascript - 不能将 Ruby Math 库与 Opal 一起使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27929098/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com