Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

I do not fully understand what am I asking (lol!), well, in the sense of if it is even possible, that is. If it isn't, sorry.

Suppose I have a Java program. It has a Main and a JavaCalculator class. JavaCalculator has some basic functions like

public int sum(int a,int b) {
   return a + b
}

Now suppose I have a ruby file. Called MyProgram.rb.

MyProgram.rb may contain anything you could expect from a ruby program. Let us assume it contains the following:

class RubyMain
  def initialize
    print "The sum of 5 with 3 is #{sum(5,3)}"
  end
  def sum(a,b)
    # <---------- Something will happen here
  end
end

rubyMain = RubyMain.new

Good. Now then, you might already suspect what I want to do:

  • I want to run my Java program
    • I want it to execute the Ruby file MyProgram.rb
    • When the Ruby program executes, it will create an instance of JavaCalculator, execute the sum function it has, get the value, and then print it.
    • The ruby file has been executed successfully.
  • The Java program closes.

Note: The "create an instance of JavaCalculator" is not entirely necessary. I would be satisfied with just running a sum function from, say, the Main class.

My question: is such possible? Can I run a Java program which internally executes a Ruby file which is capable of commanding the Java program to do certain things and get results? In the above example, the Ruby file asks the Java program to do a sum for it and give the result.


This may sound ridiculous. I am new in this kind of thing (if it is possible, that is).

WHY AM I ASKING THIS?

I have a Java program, which is some kind of game engine. However, my target audience is a bunch of Ruby coders. I don't want to have them learn Java at all. So I figured that perhaps the Java program could simply offer the functionality (capacity to create windows, display sprites, play sounds...) and then, my audience can simply code with Ruby the logic, which basically justs asks my Java engine to do things like displaying sprites or playing sounds.

That's when I though about asking this.

share|improve this question
    
You have accepted an answer based on JRuby, which is a good solution to a very specific problem. There are many other more general answers that apply to this and similar situations, but if you want to see them, you will probably now have to ask the question again in a different format. –  david.pfx Mar 29 at 6:35
add comment

2 Answers

up vote 9 down vote accepted

What you're probably really after is JRuby --> http://jruby.org/

JRuby is a fully featured, compliant Ruby implementation on the Java Virtual Machine. According to some Rubyists, it also happens to be the fastest (assuming you're running on a Java 7 compatible VM). JRuby can call into Java libraries and take return values from those calls, in short you can have the best of both worlds.

share|improve this answer
    
While this sounds promising, I would appreciate a solution to my current scenario (I have plain Java and plain Ruby), without the usage of a "different tool" like JRuby. –  Omega Dec 20 '12 at 19:12
    
JRuby will run your plain Ruby code on the JVM - your developers can still use Ruby :-). –  Martijn Verburg Dec 22 '12 at 9:37
    
JRuby is the way to go! –  akuhn Dec 24 '12 at 4:45
    
would you mind explaining more on what it does and what it's good for? "Link-only answers" are not quite welcome at Stack Exchange –  gnat Mar 31 '13 at 2:41
1  
Right you are - slightly expanded answer - HTH! –  Martijn Verburg Apr 1 '13 at 9:42
add comment

Since you don’t seem to believe us.

Here's how to run ruby code from Java, using JRuby. JRuby is not “a different tool” it is just the JAR file you need to be able to run Ruby on top of Java. It’s as simple as

package com.example;

import org.jruby.embed.LocalVariableBehavior;
import org.jruby.embed.ScriptingContainer;

public class Main {

    private ScriptingContainer ruby;

    public static void main(String[] args) {
        new Main().run();
    }

    public void run() {
        ruby = new ScriptingContainer(LocalVariableBehavior.PERSISTENT);
            // Assign the Java objects that you want to share
        ruby.put("main", this);
            // Execute a script (can be of any length, and taken from a file)
        Object result = ruby.runScriptlet("main.hello_world");
            // Use the result as if it were a Java object
        System.out.println(result);
    }

    public String getHelloWorld() {
        return "Hello, worlds!";
    }

}

As you can see it is even so smart to translate from Ruby’s hello_world to Java’s getHelloWorld naming convention.

share|improve this answer
    
It is just a JAR file? Where can I find it? –  Omega Jan 5 '13 at 7:41
    
Download the "JRuby 1.7.2 Complete.jar" file from jruby.org/download (you'll also find language installations on the same page, which are useful for development, but your final system will require the complete.jar only!) –  akuhn Jan 5 '13 at 8:06
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.