JRuby Basics(JRuby基础)

翻译自SUN的官方文档
Topics
主题
• What is and what is not JRuby?
什么是JRuby?
• Why JRuby (over Ruby) & JRuby on Rails (Ruby on Rails)?
为什么用JRuby替代Ruby?
• Why (J)Ruby (over Java)?
为什么用JRuby替代java?
• Calling Java from JRuby
在JRuby里调用Java
• Calling (J)Ruby from Java
在Java里调用Ruby

We are going to cover Rails deployment over Java platform in another presentation later on.
我们将用Rails平台替代java平台。


What is & What is not JRuby?
What is JRuby?
• Java implementation of Ruby language
Ruby语言的java实现
• Started in 2002, open source, many contributors
开始于2002年,开源做了很大贡献
• Releases
版本
> June 2007: 1.0 release, focus on compatibility
2007年六月,1.0版本发布,解决兼容性问题           
> April 2008: 1.1 release, focus on performance
2008年4月,1.1版本,解决性能问题
• Aiming for compatibility with current Ruby version
与当前Ruby版本实现了兼容
> JRuby 1.1 is compatible with: Ruby 1.8.6
JRuby1.1与Ruby1.8.6兼容

What is NOT JRuby?
• JRuby is NOT an attempt to pollute Ruby
JRuby与Ruby不构成竞争
• JRuby is NOT an attempt to alter/add incompatibility
JRuby不影响修改和添加
• JRuby is NOT a silver bullet to all Ruby issues
JRuby对于Ruby的问题并不是万能的
• JRuby is NOTan “admit that Java sucks”
JRuby不是java的副产品
• JRuby is NOT slow
JRuby并不慢

JRuby Design Goals
JRuby设计原则
• Bring beauty of Ruby to Java world
把漂亮的Ruby带到java世界
• Bring Java echo-system to Ruby world
把java带到Ruby世界
• Enable highly scalable and highly performing Ruby implementation
扩展Ruby的实现

Why JRuby (over Ruby) & JRuby on Rails (over Ruby on Rails)?
Why JRuby (over Ruby)
• With JRuby you get the best of both worlds: Ruby applications and libraries, plus Java libraries. And you can access those libraries with Ruby syntax (or Java syntax, if you want).
JRuby里你能得到两个最有用的东西:Ruby的应用和库,java的库。你可以通过Ruby的语法得到这些库(如果你愿意,也可以用java的语法)
• On average JRuby, runs 2 and a half times faster than Ruby, except at startup
JRuby的运行效率比Ruby高4倍,除了在开启阶段
• In addition to native threads, JRuby supports Unicode natively
JRuby支持Unicode编码
• Code can be fully compiled ahead of time or just in time
代码可以提前编译

Why JRuby on Rails (over Ruby on Rails)
• A JRuby on Rails application can be packaged into a WAR, and then deployed onto any compliant server.
一个JRuby on Rails应用能被打成WAR包,并且部署在任何适应的服务器上。
> The packaging of creating a war file from Rails can be done with Goldspike, or with the new kid on the block: Warbler
• Can use vast array of Java libraries
能够使用大量的java库
> JPA, JTA, JMS, EJB, JDBC, JMX, JSF, etc.
> Database connections are made through JDBC, which provides broader, more scalable database support.
可以使用JDBC
• Glassfish--the Java web app server that scales well--is available as a JRuby gem.
可以使用java的web应用服务器,如Glassfish


Why (J)Ruby over Java?
Why (J)Ruby over Java?
• Ruby is fun, beautiful, and powerful language
Ruby是有趣,漂亮,强大的语言
• Ruby has features missing from Java
Ruby有一些Java没有的特性
> Closure (blocks)
> Open classes
> Meta programming
> Duck-typing
• Domain Specific Language (DSL)
> Through (J)Ruby's capability of allowing expressive method names and cushy semantics

Calling Java From JRuby
include 'java' & import
• include 'java' statement will give you access to the bundled Java libraries. However, this will not give you access to non-bundled libraries.
• The import statement is used to import a Java Class.
  1. include 'java'
  2. import 'java.lang.ArrayList'
  3. import 'javax.swing.JFrame'
  4. list = ArrayList.new
  5. frame = JFrame.new(“Ruby SWINGS!”)
  6. list << frame
  7. list.each {|f| f.set_size(200,200) }

include_package within a Ruby Module
include在Ruby模块里的使用规则
• Use include_package "<package_name>" in a Ruby Module to support namespaced access to the Java classes in the package. It is also legal to use import "<package_name>".
  1. include Java
  2. module JavaLang
  3. include_package "java.lang"
  4. end
  5. s = JavaLang::String.new("This is my string from java.lang
  6. package")
include_class
• Use include_class "<class_name>" to include unbundled Java classes
• The unbundled Java classes (in the form of jar file) should be in the classpath
  1. include Java
  2. include_class 'mypackage.Hello'
  3. h = Hello.new
  4. puts "----Invoke a method of from Hello object"
  5. s = h.sayHello("Message from Hello Java Class!")

Java Classes Naming in JRuby
• Imported classes are mapped into Ruby name space as follows:
把java的类映射到Ruby的命名空间:
> Java Convention: org.foo.department.Widget
java规范
> JRuby convention: Java::OrgFooDepartment::Widget
JRuby规范
  1. # The following two are equivalent
  2. frame = javax.swing.JFrame.new("Passion!")
  3. frame = Java::JavaxSwing::JFrame.new("Passion!")
Java Method Naming in JRuby
• Ruby method naming convention “lower case with _” can be used for Java method
Ruby的方法用下划线_命名可以被java方法使用
version = System.getProperties["java.runtime.version"]
version = System.get_properties["java.runtime.version"]

Calling Ruby from Java
Calling Ruby From Java
• Leveraging scripting framework (JSR 223)
  1. import javax.script.*;
  2. public class EvalScript {
  3. public static void main(String[] args) throws Exception {
  4. ScriptEngineManager factory = new ScriptEngineManager();
  5. // Create a JRuby engine.
  6. ScriptEngine engine = factory.getEngineByName("jruby");
  7. // Evaluate JRuby code from string.
  8. try {
  9. engine.eval("puts('Hello')");
  10. catch (ScriptException exception) {
  11. exception.printStackTrace();
  12. }
  13. }

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值