不能将 Ruby 数学库与 Opal 一起使用

Cannot use Ruby Math library with Opal

本文关键字:Opal 一起 Ruby 不能      更新时间:2023-09-26

有没有办法在Opal中使用Ruby Math库?

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

红宝石代码:

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')build到文件中。

但是,对于您的特定示例,您可以只使用 JS PI 近似值(无论如何,这就是 Opal Math::PI所做的一切):

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