使用Clojure的gem中提供的JRuby类

时间:2021-05-26 22:51:06

I am a fairly simple need to use a Ruby class from within Clojure. The complicating factors are that the class is supplied in a gem. The optimal approach would be to setup my Leiningein project file along the lines of:

我很简单地需要在Clojure中使用Ruby类。复杂的因素是该类是在宝石中提供的。最佳方法是按照以下方式设置我的Leiningein项目文件:

(project foo ""
  ...
  :dependencies [[clojure ...]
                 [jruby ...  ]])

Likewise I would prefer to simply check the gem and its dependencies into the local repo directory. Therefore, from my ideal usage would then be:

同样,我更愿意只检查gem及其依赖项到本地repo目录中。因此,从我的理想用法将是:

(ns bar.baz
  (require [jruby.something :as jruby])

(def obj (jruby/CreateAnInstance "TheGemClass"))

(def result (jruby/CallAMethod obj "method_name" some args))

Thanks.

谢谢。

1 个解决方案

#1


10  

Here's a short list of steps to get the hello-world gem running using JRuby and Clojure and a few references. In fact, the steps compose just a short sketch of how the material from the references might come together (with some project.clj entries). The first reference, Haml on Clojure Web App on Yoko Harada's (@yokolet's) blog, uses a slightly different way actually to call into JRuby, but includes the key remark on how to write require("...") lines for use with JRuby and gems on the classpath.

下面是使用JRuby和Clojure以及一些参考资料来运行hello-world gem的简短步骤列表。实际上,这些步骤只是简单描述了引用中的材料如何组合在一起(使用一些project.clj条目)。第一个参考,在Yoko Harada(@ yokolet's)博客上的Clojure Web App上的Haml,使用了一种稍微不同的方式来调用JRuby,但包括关于如何编写require(“...”)行以供使用的关键注释JRuby和类路径上的宝石。

  1. Add [org.jruby/jruby-complete "1.6.7.2"] to your :dependencies and have Leiningen fetch the dependencies.

    将[org.jruby / jruby-complete“1.6.7.2”]添加到您的:dependencies并让Leiningen获取依赖项。

  2. Create a gems directory in the project root and add it to :resource-paths in your project.clj This requires Leiningen 2. See the Leiningen source for the correct format.

    在项目根目录中创建一个gems目录并将其添加到:project.clj中的resource-paths这需要Leiningen 2.请参阅Leiningen源代码以获取正确的格式。

  3. Say

    # see reference 4
    GEM_HOME=gems GEM_PATH=gems java -jar ~/.m2/repository/org/jruby/jruby-complete/1.6.7.2/jruby-complete-1.6.7.2.jar -S gem install hello-world
    

    in the project root.

    在项目根目录中。

  4. Start up the REPL service of your choice with GEM_HOME and GEM_PATH set as above. (I tested this with lein2 swank.)

    使用上面设置的GEM_HOME和GEM_PATH启动您选择的REPL服务。 (我用lein2 swank测试了这个。)

  5. Say the following at the REPL:

    在REPL中说出以下内容:

    ;;; see reference 2, first snippet
    (let [runtime (JavaEmbedUtils/initialize (list))
          evaler  (JavaEmbedUtils/newRuntimeAdapter)]
      (doseq [ruby-expr ["require('rubygems')"
                         "require('gems/hello-world-1.2.0/lib/hello-world')"]]
        (.eval evaler runtime ruby-expr)))
    
  6. Behold the nil return value, as well as a couple of lines printed out to the terminal the REPL service has been started from.

    看看nil返回值,以及打印到REPL服务的终端打印的几行。

References:

参考文献:

  1. Haml on Clojure Web App on Yoko Harada's (@yokolet's) blog
  2. 在Yoko Harada(@ yokolet's)博客上的Clojure Web App上的Haml
  3. JRuby 1.1.6: Gems-in-a-jar on Nick Sieger's blog
  4. JRuby 1.1.6:Nick Sieger博客上的Gems-in-a-jar
  5. DirectJRubyEmbedding on the JRuby Wiki at Project Kenai
  6. 在Kenai项目的JRuby Wiki上的DirectJRubyEmbedding
  7. consuming gems from jruby-complete here on SO (note the comments)
  8. 在这里消费来自jruby-complete的宝石(请注意评论)

#1


10  

Here's a short list of steps to get the hello-world gem running using JRuby and Clojure and a few references. In fact, the steps compose just a short sketch of how the material from the references might come together (with some project.clj entries). The first reference, Haml on Clojure Web App on Yoko Harada's (@yokolet's) blog, uses a slightly different way actually to call into JRuby, but includes the key remark on how to write require("...") lines for use with JRuby and gems on the classpath.

下面是使用JRuby和Clojure以及一些参考资料来运行hello-world gem的简短步骤列表。实际上,这些步骤只是简单描述了引用中的材料如何组合在一起(使用一些project.clj条目)。第一个参考,在Yoko Harada(@ yokolet's)博客上的Clojure Web App上的Haml,使用了一种稍微不同的方式来调用JRuby,但包括关于如何编写require(“...”)行以供使用的关键注释JRuby和类路径上的宝石。

  1. Add [org.jruby/jruby-complete "1.6.7.2"] to your :dependencies and have Leiningen fetch the dependencies.

    将[org.jruby / jruby-complete“1.6.7.2”]添加到您的:dependencies并让Leiningen获取依赖项。

  2. Create a gems directory in the project root and add it to :resource-paths in your project.clj This requires Leiningen 2. See the Leiningen source for the correct format.

    在项目根目录中创建一个gems目录并将其添加到:project.clj中的resource-paths这需要Leiningen 2.请参阅Leiningen源代码以获取正确的格式。

  3. Say

    # see reference 4
    GEM_HOME=gems GEM_PATH=gems java -jar ~/.m2/repository/org/jruby/jruby-complete/1.6.7.2/jruby-complete-1.6.7.2.jar -S gem install hello-world
    

    in the project root.

    在项目根目录中。

  4. Start up the REPL service of your choice with GEM_HOME and GEM_PATH set as above. (I tested this with lein2 swank.)

    使用上面设置的GEM_HOME和GEM_PATH启动您选择的REPL服务。 (我用lein2 swank测试了这个。)

  5. Say the following at the REPL:

    在REPL中说出以下内容:

    ;;; see reference 2, first snippet
    (let [runtime (JavaEmbedUtils/initialize (list))
          evaler  (JavaEmbedUtils/newRuntimeAdapter)]
      (doseq [ruby-expr ["require('rubygems')"
                         "require('gems/hello-world-1.2.0/lib/hello-world')"]]
        (.eval evaler runtime ruby-expr)))
    
  6. Behold the nil return value, as well as a couple of lines printed out to the terminal the REPL service has been started from.

    看看nil返回值,以及打印到REPL服务的终端打印的几行。

References:

参考文献:

  1. Haml on Clojure Web App on Yoko Harada's (@yokolet's) blog
  2. 在Yoko Harada(@ yokolet's)博客上的Clojure Web App上的Haml
  3. JRuby 1.1.6: Gems-in-a-jar on Nick Sieger's blog
  4. JRuby 1.1.6:Nick Sieger博客上的Gems-in-a-jar
  5. DirectJRubyEmbedding on the JRuby Wiki at Project Kenai
  6. 在Kenai项目的JRuby Wiki上的DirectJRubyEmbedding
  7. consuming gems from jruby-complete here on SO (note the comments)
  8. 在这里消费来自jruby-complete的宝石(请注意评论)