Hello everyone, I'm currently developing a Java application and I know a bit of Ruby. I've stumbled across JRuby when I was looking for a lightweight scripting language. In my application, the end-user should be able to attach small scripts at certain points in the application. On the Java side, the script should be stored as a simple string (containing valid Ruby syntax) and then be executed at runtime. Now I do have some questions here... 1) Is the above scenario possible with JRuby or am I using the wrong tool? 2) How does JRuby inter-operate with Java, i.e. is it capable of calling Java methods, access the current Java call stack (i.e. local variables, static fields), work with Java data structures / classes...? Will this work at all? I've never integrated a scripting language in any of my applications so far, sorry if the questions sound a little "noobish". I'm not even sure if JRuby is the right tool for this job. I've read most of the contents of the JRuby homepage, but I couldn't find an answer to those questions. I'd be grateful for any advice! Alan
on 2014-05-11 20:39
on 2014-05-11 23:10
Alan - You can certainly call JRuby from Java. The Using JRuby book has a chapter on how to do this (http://pragprog.com/book/jruby/using-jruby). If you'll be do any significant amount of work with JRuby, I recommend this book. Basically, you create a scripting container, set it up with context (variables, load paths, etc.), then tell it to run your script. The Ruby code calls Java methods pretty seamlessly, except for some obscure exceptions, such as specifying a method signature where the Java method is overloaded. I think that other than the context you explicitly pass to the scripting container, your Ruby code will have no knowledge of what the Java app is doing. - Keith Keith R. Bennett http://about.me/keithrbennett
on 2014-05-11 23:39
Hi Keith, thank you for your response and the link in the book, I'm going to take a look at it! JRuby does indeed sound *very* promising. Thanks! Alan
on 2014-05-12 01:35
Hi all, just as a follow up to the Keith answear, i'm using this scenario in my apps for years now, it's based on jsr-223 and scripting engine. But if i remember correctly there is something called redbridge  which gives you way more control.  - https://github.com/jruby/jruby/wiki/RedBridge Best greetings, Paweł Wielgus. ps. is it possible you can share what for will you use it? i'm asking because after i switched one of my apps from in-memory database (prevayler) into relational database the need for this scripting engine was gone for good, so it would be quite interesting what other bussines need can be acomplished by this solution. 2014-05-11 23:39 GMT+02:00 Alan DW <email@example.com>:
on 2014-05-12 15:12
> But if i remember correctly > there is something called redbridge  > which gives you way more control. > >  - https://github.com/jruby/jruby/wiki/RedBridge I will take a look at it, thanks :) > ps. is it possible you can share what for will you use it? > i'm asking because after i switched one of my apps > from in-memory database (prevayler) into relational database > the need for this scripting engine was gone for good, > so it would be quite interesting what other bussines need can be > acomplished by this solution. Well, I guess it's not strictly "business". What I want to do is to take libGDX, a rendering engine for Java built on top of LWJGL, and build a level editor for it. In order to allow users to define events that should happen in-game, a lightweight scripting language seems apropriate to me. At least in cases which are too small or too simple to create a full-fledged Java extension.
on 2014-05-12 21:14
> > >  - https://github.com/jruby/jruby/wiki/RedBridge > I will take a look at it, thanks :) As you can see there, RedBridge is simply org.jruby.embed these days, which embedded itself too:) into Jruby. AFAIK there is nothing extra to be installed for running Ruby inside a java App - is there? BTW, I used to use JSR223, but realised it wasn't worth the hassle for me. The core embed API is simpler, cleaner and more powerful - unless you need to support other languages so you are ready to take other kinds of compromises.