以前做了个Ruby的项目,最近有了点新的需求,其中有一条是要保护源代码,我选择用JRuby把rb文件编译为class,查了些资料,看了下jrubyc相关的一些代码,也许有朋友有类似的需要,和大家分享一下。突发奇想用英文来写,很久不写英文了,果然这个还得常写写。。。附上对应的中文,如果不高兴看英文的同学可以直接看中文的,呵呵。
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 <rb>
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\jrubyc src\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 DIR |
Use DIR as the root of the compiled package and filename |
-p,--prefix PREFIX |
Prepend PREFIX to the file path and package. "ruby" is default |
-t,--target TARGET |
Output 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.
分享到:
- 2008-06-02 16:55
- 浏览 1691
- 评论(3)
- 论坛回复 / 浏览 (3 / 3068)
- 查看更多
相关推荐
Engineering-Modeling-Languages-Turning-Domain-Knowledge-into-Tools.pdf
AS-23-Valsamaras-Dirty-Stream-Attack-Turning-Android
「大数据」hum-t10-turning-your-security-strategy-inside-out-managing-insider-threat - 安全认证 数据库安全 安全研究 应急响应 零信任 安全资讯
P-tuningP-tuning方法在中文上的简单实验。方法依赖bert4keras 0.10.4交流QQ交流群:808623966,微信群请加机器人微信号spaces_ac_cn
本项目提供的"vision-measurement-of-turning-tool.rar"压缩包文件包含了利用MATLAB实现的车刀视觉测量算法,特别是关注于亚像素级别的精度提升。 MATLAB是一款强大的数值计算和数据可视化软件,它在图像处理和...
### Java优化方法详解 #### 一、性能调试的艺术 在《寻找你的J2EE应用中存在的性能瓶颈》这一讲座中,Sun公司的资深系统工程师Charlie Hunt、Eileen Loh和Scott Oaks分享了他们对于Java性能调试的独特见解。他们...
信息安全与数据安全是现代IT领域中极为重要的议题,它们覆盖了从策略制定到技术实施的广泛内容。本文档内容涉及了工业网络的安全、威胁情报的收集与分析、AI在安全领域的应用、移动安全等众多方面。...
本文所讨论的研究成果,即“Turning Mobile Phones into 3D Scanners”,便是在这一趋势下的具体体现。 该研究的背景和动机在于对简单可靠地生成现实世界物体和环境的3D模型的需求日益增长。传统的激光扫描技术虽然...
Turning Smart phones into Robot; Currentrobotsareeitherexpensiveormakesignificantcompromiseson sensory richness, computational power, and communication capabilities. We propose to leverage smartphones...
Refactoring JavaScript Turning Bad Code Into Good Code 英文mobi 本资源转载自网络,如有侵权,请联系上传者或csdn删除 本资源转载自网络,如有侵权,请联系上传者或csdn删除
Written by consultants from the Software Improvement Group (SIG), this book provides clear and concise explanations, with advice for turning the guidelines into practice. Examples for this edition are...
Refactoring JavaScript: Turning Bad Code Into Good Code by Evan Burchard English | 13 Mar. 2017 | ASIN: B06XK1V629 | 442 Pages | AZW3 | 1.8 MB How often do you hear people say things like this? "Our ...
「NGFW」Incident Response Metrics Turning Stats into Stories - 工控安全 安全教育 等级保护 APT攻击 数据安全 数据库审计
标题“人工智能图片写html”揭示了一个新兴的技术领域,即将图像转换为HTML代码,这主要得益于深度学习技术的进步。深度学习是人工智能的一个分支,通过模仿人脑神经网络的工作方式,能够从大量数据中学习并自动执行...
This book is an approachable, step-by-step introduction into programming with Swift for everyone. It begins by giving you an overview of the key features through practical examples and progresses to ...
《信息安全与数据安全:GLitch Chronicles - Turning WebGL into a Hammer》 在当今信息化社会,信息安全与数据安全成为了企业和个人必须面对的重要课题。GLitch Chronicles 是一个揭示网络安全问题的研究项目,它...
本书《Refactoring JavaScript: Turning Bad Code Into Good Code》是由Evan Burchard所著,旨在教授JavaScript开发者如何通过重构代码来改进和优化程序。书中的内容涵盖了重构的基础知识、原则以及具体的重构技术,...
无路可退 ... 二手技术和资料库: 带有 带有模板引擎的 预处理器 要部署此游戏,您将需要已安装node.js和npm 。... 另外,您将需要安装ruby和sass gem 。 或者,您可以运行我们的deploy.sh文件。