开发者

Installing gems inside (J)Ruby code

开发者 https://www.devze.com 2022-12-20 12:13 出处:网络
I am using JRuby along with Cucumber and is looking for a way of running jruby -S gem update --system

I am using JRuby along with Cucumber and is looking for a way of running

 jruby -S gem update --system
 jruby -S gem install cucumber

from within the Java ScriptEngine. No amount of Googling have let me to a solution to this problem. Basically I want to be able to do something like this

 ScriptEngineManager manager = new ScriptEngineManager();
 ScriptEngine jRubyEngine = manager.getEngineByName("jruby");
 : // 开发者_开发技巧some unknown code here
 jRubeEngine.eval("call gems install/update from inside JRuby")

Is there a way of accomplishing this?


RubyGems is just a Ruby library. The gem command is only a thin wrapper around the library. Everything you can do with the command, you can do with the library.

I've never actually used the library, but I guess what you want to look at is the Gem::DepencyInstaller and the code would look something like this (completely untested, just pulled out of my you-know-what):

ScriptEngineManager manager = new ScriptEngineManager();
ScriptEngine jRubyEngine = manager.getEngineByName("jruby");
String s = "
  require 'rubygems'
  require 'rubygems/dependency_installer'
  Gem::DependencyInstaller.new.install('cucumber')
";
jRubyEngine.eval(s);
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号