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:
(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:
(ns bar.baz
(require [jruby.something :as jruby])
(def obj (jruby/CreateAnInstance "TheGemClass"))
(def result (jruby/CallAMethod obj "method_name" some args))
Thanks.
Here’s a short list of steps to get the
hello-worldgem 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 someproject.cljentries). 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 writerequire("...")lines for use with JRuby and gems on the classpath.Add
[org.jruby/jruby-complete "1.6.7.2"]to your:dependenciesand have Leiningen fetch the dependencies.Create a
gemsdirectory in the project root and add it to:resource-pathsin yourproject.cljThis requires Leiningen 2. See the Leiningen source for the correct format.Say
in the project root.
Start up the REPL service of your choice with
GEM_HOMEandGEM_PATHset as above. (I tested this withlein2 swank.)Say the following at the REPL:
Behold the
nilreturn value, as well as a couple of lines printed out to the terminal the REPL service has been started from.References: