如何使用ruby gem commonjs加载纯JS文件? [英] How to load pure JS file using ruby gem commonjs?

查看:143
本文介绍了如何使用ruby gem commonjs加载纯JS文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

简单问题:如何从ruby加载纯.js文件?

Simple question: how to load pure .js file from ruby?

我当前的策略是从内部加载.js文件ruby是使用 commonjs gem 。我发现说明是加载 js目录,然后需要文件。不幸的是,当我尝试加载目录以尝试将nil转换为字符串时,我不断收到TypeError。

My current strategy is to load a .js file from within ruby is to use the commonjs gem. I recognize that the instructions are to load the js directory and then require the file. Unfortunately, I keep getting a TypeError when I try to load the directory for trying to convert nil to string.

这是我的文件结构:

.
├── Gemfile
├── LICENSE.md
├── README.md
├── Rakefile
├── lib
│   ├── example
│   │   ├── js
│   │   │   ├── data.json
│   │   │   ├── expressions.js
│   │   │   └── javascript_i_want_to_run.js
│   │   └── version.rb
│   └── example.rb
├── example.gemspec
├── script
└── test

commonjs指令,这是我从pry调用时遇到的失败(注意我正在加载目录而不是文件)。

Following the commonjs instructions, here is the failure I get when invoking from pry (notice I'm loading the directory not the file).

[1] pry(main)> require 'commonjs'
=> true
[2] pry(main)> env = CommonJS::Environment.new(path: 'lib/example/js')
TypeError: no implicit conversion of nil into String
from /usr/local/rvm/gems/ruby-2.1.5@rails4/gems/commonjs-0.2.7/lib/commonjs/environment.rb:9:in `initialize'

我很难过。有什么想法会发生什么?

I'm stumped. Any ideas what could be happening?

推荐答案

自述文件中有错误。 初始化方法中的第一个参数 CommonJS :: Environment 类是 runtime 。它应该是:

There is a mistake in readme. The first parameter in initialize method of CommonJS::Environment class is runtime. It should be:

require 'commonjs'
require 'v8'

runtime = V8::Context.new
env = CommonJS::Environment.new(runtime, path: 'lib/example/js')

这篇关于如何使用ruby gem commonjs加载纯JS文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆