From: Glen Starchman Date: 2001-05-17T02:08:01+09:00 Subject: [ruby-talk:15301] Re: Ruby in Java > I'm not familiar with Jython, but providing a BSF compatible interpreter > would have certain benefits; primarily, that many existing applications could > automatically plug the Ruby interpreter into themselves. Jython allows you to write code that looks exactly like Python. For example: class test: def __init__(self): print "initializing!" def test_func(self, x): print x which would (roughly) translate to the following Java: import java.lang.*; import PyCore.*; /* import a bunch of Jython support classes here... */ public class test { test() { PyString s1 = new PyString("initializing!"); System.out.println(s1); } public void test_func(PyObject x) { System.out.println(x); } } where PyString and PyObject have a toString() method that allows them to work with println. You can see that in a large project you end up with lots of code like PyString py=new PyString(...) and the like. Pretty ugly stuff... > > > 1) a Ruby interpreter, and all supporting libraries, ported to Java > > 2) A Ruby-Java "translator" that makes use of the above libraries and > > interfaces seamlessly with Java. > > Is (2) a Ruby-to-Java translator? And, by (1), I'm assuming you mean that > you could do all of the following: > > 1) String result = ruby.Ruby.eval( " %w{a b c d e f}.find{|n| n == 'c'} " ); > // => "c" Sure. You should have access to the Ruby eval-print loop. > 2) $ java -cp ruby.jar irb.rb > irb(main):001:0> p "Hello World" > "Hello World" > nil In jython the paradigm is: $jython Jython 2.1a1 on java1.3.0_02 (JIT: null) ... One thing that Jython does do very well is package up all of the necessary code into a nice little jar files... with the -c and -d options, it also takes care of bundling up all of the necessary Jython support files. So, after compiliation and bundling, you get a true Java jar that is not dependent on the end user having Jython (or Python) installed: $java -jar JythonApp.jar > irb(main):002:0> > 3) String result = (new ruby.Regex("/(.)(.)(.)/")).match("abc")[2]; > System.out.println( result ); // => "b" I think the code would end up looking something like: import ruby; ... ruby.Regex regex = new ruby.Regex(/(.)(.)(.)/")); List results = (List)regex.match("abc"); String result = results.elementAt(2); (or somethink like that!) from "pure" Ruby code. > I'm sure the last would be useful, although you'd lose most of the Ruby good > stuff. You don't necessarily have to, I don't think... although things like code blocks would be a very trying thing to implement in Java... ;-)