introduction to jruby and jruby on rails
TRANSCRIPT
![Page 1: Introduction to JRuby And JRuby on Rails](https://reader036.vdocuments.us/reader036/viewer/2022082502/5555a79fd8b42afe5d8b45ab/html5/thumbnails/1.jpg)
Introduction to JRuby
Design by Mr.Huân
![Page 2: Introduction to JRuby And JRuby on Rails](https://reader036.vdocuments.us/reader036/viewer/2022082502/5555a79fd8b42afe5d8b45ab/html5/thumbnails/2.jpg)
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](https://reader036.vdocuments.us/reader036/viewer/2022082502/5555a79fd8b42afe5d8b45ab/html5/thumbnails/3.jpg)
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](https://reader036.vdocuments.us/reader036/viewer/2022082502/5555a79fd8b42afe5d8b45ab/html5/thumbnails/4.jpg)
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](https://reader036.vdocuments.us/reader036/viewer/2022082502/5555a79fd8b42afe5d8b45ab/html5/thumbnails/5.jpg)
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](https://reader036.vdocuments.us/reader036/viewer/2022082502/5555a79fd8b42afe5d8b45ab/html5/thumbnails/6.jpg)
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](https://reader036.vdocuments.us/reader036/viewer/2022082502/5555a79fd8b42afe5d8b45ab/html5/thumbnails/7.jpg)
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](https://reader036.vdocuments.us/reader036/viewer/2022082502/5555a79fd8b42afe5d8b45ab/html5/thumbnails/8.jpg)
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](https://reader036.vdocuments.us/reader036/viewer/2022082502/5555a79fd8b42afe5d8b45ab/html5/thumbnails/9.jpg)
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](https://reader036.vdocuments.us/reader036/viewer/2022082502/5555a79fd8b42afe5d8b45ab/html5/thumbnails/10.jpg)
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](https://reader036.vdocuments.us/reader036/viewer/2022082502/5555a79fd8b42afe5d8b45ab/html5/thumbnails/11.jpg)
Demo
![Page 12: Introduction to JRuby And JRuby on Rails](https://reader036.vdocuments.us/reader036/viewer/2022082502/5555a79fd8b42afe5d8b45ab/html5/thumbnails/12.jpg)
Thank for watching