12
Introduction to JRuby Design by Mr.Huân

Introduction to JRuby And JRuby on Rails

Embed Size (px)

Citation preview

Page 1: Introduction to JRuby And JRuby on Rails

Introduction to JRuby

Design by Mr.Huân

Page 2: Introduction to JRuby And JRuby on Rails

What is JRuby?

JRuby is an implementation of Ruby programming language that runs on the Java Virtual Machine(JVM).

Started in 2002, open source, many contributors.

Releases

> June 2007: 1.0 release, focus on compatibility.

> April 2008: 1.1 release, focus on performance.

Aiming for compatibility with current Ruby version.

> JRuby 1.7.6 is compatible with: Ruby 1.9.3

Page 3: Introduction to JRuby And JRuby on Rails

Why Use JRuby?

• 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).

• On average JRuby, runs 2 and a half times faster than Ruby, except at startup.

• In addition to native threads, JRuby supports Unicode Natively.

• Code can be fully compiled ahead of time or just in time.

Page 4: Introduction to JRuby And JRuby on Rails

Install JRuby and JRuby Gems

• Download and install JRuby

- Download: http://jruby.org/download

- Instructions: https://github.com/jruby/jruby/wiki/GettingStarted

- In linux : rvm install jruby or rbenv install jruby

• Install JRuby Gems:

- jruby -S gem install rails mongrel

Page 5: Introduction to JRuby And JRuby on Rails

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.

include Java

import java.util.ArrayList

import javax.swing.JFrame

list = ArrayList.new

frame = javax.swing.JFrame.new("Passion!")

list << frame

list.each {|f| f.set_size(200,200) }

Page 6: Introduction to JRuby And JRuby on Rails

Calling Java From JRuby (cont)

include_package within a JRuby Module:

• Use include_package"<package_name>"in a JRuby Module to support namespaced access to the Java classes in the package.

include Java

module JavaLang

include_package "java.lang"

end

s = JavaLang::String.new("This is my string from java.lang package")

Page 7: Introduction to JRuby And JRuby on Rails

Calling Java From JRuby (cont)

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.

include Java

include_class 'mypackage.Hello'

h = Hello.new

puts "----Invoke a method of from Hello object"

s = h.sayHello("Message from Hello Java Class!")

Page 8: Introduction to JRuby And JRuby on Rails

JRuby on Rails

You can use JRuby with Ruby on Rails. JRuby gives Rails the power and functionality of the Java Platform, providing it with:

• Excellent garbage collection for endless uptimes.

• Hotspot profiled dynamic optimizations for great performance.

• Access to the Java ecosphere for additional technology options.

• Deployment to Java application servers for ubiquity.

Page 9: Introduction to JRuby And JRuby on Rails

JRuby on Rails

• Create a new JRuby on Rails app:

- gem install rails

- rails new blog\

- rails new my_app -m http://jruby.org/rails3.rb

• /Gemfile

- gem 'activerecord-jdbcsqlite3-adapter‘

- gem 'jruby-openssl'

- gem 'therubyrhino'

Page 10: Introduction to JRuby And JRuby on Rails

Deployment

•  Use JRuby, you can use a Java application servers: Tomcat , Glassfish, Jetty, Jboss…

• To deploy to a Java app server, you can use the tool Warbler to bundle your Rails application in a Java Web Application Archive.

• Once you have a .war file, you can deploy to any Java app server using its war deployment mechanism

Page 11: Introduction to JRuby And JRuby on Rails

Demo

Page 12: Introduction to JRuby And JRuby on Rails

Thank for watching