您的位置:首页 > 编程语言 > Ruby

Step by Step - Turning Ruby Files into Java Classes(中英文对照版)

2008-06-02 21:48 417 查看
There are many reasons for us to turn Ruby files (.rb) into Java classes (.class). For example, you want a high-performance system, or you want to protect your ruby code, etc. Last year, JRuby finished its compiler which can help you to do the compilation. Now you’ve got another choice besides XRuby. You can find some material on JRuby’s Wiki, but it’s far from enough, and the most pages you find on the Internet just compile some hello world programs. I tried to compile my ruby project (of course, it's more complex than printing "Hello World", it's a network traffic control script under FreeBSD) last week. If you have the same requirement, maybe my experience can give you a little help :-)

The first question is how to start the compiler. The Wiki just tells us to use "jrubyc". Actually, there's no jrubyc.bat or jubyc.exe in bin at all. So, if you are using Windows like me, you should start the script jrubyc manually. For example:

jruby %JRUBY_HOME%/bin/jrubyc

Suppose your program is very simple, please forgive me using "Hello World" again. (src/hello.rb)

p "Hello World"

The command to compile it looks like this:

jruby %JRUBY_HOME%/bin/jrubycsrc/hello.rb 

Then, try this one to run it:

java -cp .;%JRUBY_HOME%/lib/jruby.jar ruby.src.hello

Have you feel something strange? Why the class is ruby.src.hello? After checking the command line parameters, you will find the answer. Here is the command pattern:

jrubyc [options] (FILE|DIRECTORY)

jrubyc has three parameters:

-d,--dir DIRUse DIR as the root of the compiled package and filename
-p,--prefix PREFIXPrepend PREFIX to the file path and package. "ruby" is default
-t,--target TARGETOutput files to TARGET directory
Now, try to compile the hello.rb again:

jruby %JRUBY_HOME%/bin/jrubyc-d src -p "" src/hello.rb
java -cp .;%JRUBY_HOME%/lib/jruby.jar hello

Next, find yourself some other Ruby files which have require or include or something else. Put them into a directory. Let's call it dir. Try the following command:

md classes
jruby %JRUBY_HOME%/bin/jrubyc-t classes -d dir -p "" dir

If everything goes right, you can check the classes directory. All the files are there. Since you require something outside the source files, don’t forget to tell java where to find them. For example, your dir/complex_ruby_script required yaml.

java -cp ./classes;%JRUBY_HOME%/lib/jruby.jar;%JRUBY_HOME%/lib/ruby/1.8 complex_ruby_script

You should see the result of your Ruby program. Maybe I should say your Java program.

That's all. If you want to compile your Rails project so as to deploy it into a Java EE application server, please check out GoldSpike and Warbler.

 

 

有很多原因促使我们将Ruby文件(.rb)编译成Java的类,可以是追求更高的性能,也可是出于要保护源代码的目的等等。去年,JRuby完成了它的编译器,现在你有了XRuby以外的另一个选择。在JRuby的Wiki上有些材料,不过那还远远不够,而在Internet上找到的大多数东西又只是简单地编译hello world程序。上周我试着编译了一下我的一个ruby项目(当然,它做的事情比打印“Hello World”复杂多了,那是一个FreeBSD下的流量控制脚本)。如果你也有类似的需求,希望我的经验能对你有所帮助:-)

首先是如何启动编译器。Wiki上只是告诉我们使用“jrubyc”。实际上bin目录里根本就没有jrubyc.bat或者是jrubyc.exe。所以,如果你和我一样在使用Windows,那就需要手动启动jrubyc脚本。例如:

jruby %JRUBY_HOME%/bin/jrubyc

假设你的程序很简单,请原谅我在这里又一次使用了“Hello World”。 (src/hello.rb)

p "Hello World"

用如下命令进行编译:

jruby %JRUBY_HOME%/bin/jrubycsrc/hello.rb 

然后,这样来执行它:

java -cp .;%JRUBY_HOME%/lib/jruby.jar ruby.src.hello

是不是觉得有点奇怪?为什么这个类是ruby.src.hello?在看过命令行参数后你就知道了。它的命令行格式如下:

jrubyc [options] (FILE|DIRECTORY)

jrubyc有3个参数:

-d,--dir DIR将DIR作为编译后的包和文件的根目录
-p,--prefix PREFIX将PREFIX作为文件或包的路径的前缀。默认是“ruby”
-t,--target TARGET将文件输出到TARGET目录
现在,重新编译一下src/hello.rb:

jruby %JRUBY_HOME%/bin/jrubyc-d src -p "" src/hello.rb
java -cp .;%JRUBY_HOME%/lib/jruby.jar hello

接下来,找些有require、include或别的什么东西的Ruby文件。把它们放到一个目录里,假设名字是dir。运行如下命令:

md classes
jruby %JRUBY_HOME%/bin/jrubyc-t classes -d dir -p "" dir

如果一切正常,看一下classes目录,所有的文件都在那里了。因为你在源代码中引用了些别的东西,请一定告诉java到哪里去寻找这些文件。例如dir/complex_ruby_script里require 了yaml。

java -cp ./classes;%JRUBY_HOME%/lib/jruby.jar;%JRUBY_HOME%/lib/ruby/1.8 complex_ruby_script

此时应该可以看到Ruby程序的结果,也许我该说是Java程序的:-)

这就是全部内容了。如果你希望编译Rails项目,然后把它部署到Java EE应用服务器中,请查阅GoldSpike和Warbler以获得更多信息。

P.S. CSDN Blog的代码样式貌似出了点问题,我在JavaEye也贴了一遍,可以访问http://www.javaeye.com/topic/199480
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息