SlideShare a Scribd company logo
JRuby -- Java/Ruby Synergy


             Keith Bennett
     keithrbennett --at-- gmail.com
           @keithrbennett
What is JRuby?

An implementation of the Ruby language on the Java
Virtual Machine:

•   Open Source
•   Main site at http://jruby.org/
•   Source Code at https://github.com/jruby/jruby
•   Matz writes C so we don't have to;
    the JRuby team writes Java so we don't have to.
• Both1.8 and 1.9 compatible.
Non-Java Languages on the JVM

"The Java virtual machine knows nothing of the Java
programming language, only of a particular binary format,
the class file format."
- from http://java.sun.com/developer/technicalArticles/DynTypeLang/




•	

   JRuby
•	

   Scala
•	

   Clojure
•	

   Groovy
•	

   JavaScript
Technical Benefits of Running Ruby on the JVM

• mature and stable platform
• more portable – runs on older and obscure OS's that
    have JVM's
•   access to a huge number of libraries, e.g. XML, SOAP
•   excellent performance, garbage collection, and
    multithreading
•   performance tuning and profiling tools (e.g. jvisualvm)
•   excellent I18N (internationalization) support
•   highly scalable
•   can drive Java API's
Benefits of Ruby on the JVM
                for Enterprise Java Shops
• all the technical benefits previously mentioned, plus:
• can use Ruby DB tools such as Active Record (esp. useful for
    migrations), DBI, etc.
•   can introduce development staff to scripting languages in
    general, and Ruby in particular
•   can automate one-off tasks that were previously too cost
    prohibitive to automate
•   can grow Ruby expertise that can be applied to various kinds of
    tasks (e.g. testing, scripting, web development)
•   can introduce it in a way that will not be distributed with
    production code
•   can use it for saner and more rapid development of Swing or
    SWT client-side GUI apps (see JRuby as a Better Language for
    the JVM - http://krbtech.wordpress.com/2009/02/26/jruby-a-
    better-language-for-the-javavirtual-machine/
Benefits of Using JRuby for Ruby Developers

• all the technical benefits previously mentioned, plus:
• enables expanding customer base to include Java
    shops, many of which are very large and well funded.
•   enables creation of better solutions by increasing the
    set of parts that can be assembled into a solution --
    sometimes Java is better (e.g. XML, SOAP support).
Calling Java from Ruby

require 'java'
java.lang.System.properties.sort.each do |k,v|
  printf("%-30s %sn", k, v) }
end


displays:
awt.nativeDoubleBuffering      true
awt.toolkit                    apple.awt.CToolkit
file.encoding                  UTF-8
file.encoding.pkg              sun.io
file.separator                 /
gopherProxySet                 false
java.awt.printerjob            apple.awt.CPrinterJob
java.class.path
java.class.version             50.0
…
Adding Ruby methods to Java Classes

require 'java'

java_import java.lang.System

class System

  def self.properties_pp
    s = ''
    properties.sort.each do |k,v|
      s << sprintf("%-34s %sn", k, v)
    end
    s
  end
end


puts System.properties_pp
JRuby Method Generation
JRuby generates snake-cased named methods for Java
camel-case named methods, and conventional reader
and writer methods à la attr_accessor:
# The java.util.Locale class contains only getDefault and setDefault.
# JRuby adds the others:

jruby-1.6.5 :003 > puts Locale.methods.sort.grep /[Dd]efault/
default
default=
getDefault
get_default
setDefault
set_default


Also, JRuby makes the Enumerable interface available
to some kinds of Java objects, enabling the above, and
the following:
Locale.iSOCountries.each { |country_code| puts country_code }
Locale.iSOLanguages.each { |language_code| puts language_code }
JRuby to Java Type Conversions

jruby-1.6.5 :027 > 123.class
 => Fixnum
jruby-1.6.5 :028 > 123.to_java.class
 => Java::JavaLang::Long
Calling JRuby from Java

import org.jruby.embed.ScriptingContainer;


public class ScriptingContainerExample {


    public static void main(String[] args) {
        ScriptingContainer container = new ScriptingContainer();
        container.put("$greeting", "Hello from JRuby!");             // optional
        // container.setLoadPaths(aJavaListOfDirectories);           // optional
        container.setCompatVersion(org.jruby.CompatVersion.RUBY1_9); // optional
        container.runScriptlet("puts $greeting");
    }
}


// >javac -cp ".:$JRUBY_JAR" ScriptingContainerExample.java
// >java -cp ".:$JRUBY_JAR" ScriptingContainerExample
// Hello from JRuby!
Special Java Support Calls

•   java_import - Imports java classes
•   java_send - for specifying which function to call, by signature
•   java_alias - for creating an alias for a Java function with signature
•   java_method - for creating a callable reference to a Java function
    with signature
• field_accessor - for accessing Java instance variables, even private
    ones
• add_class_annotation - Adds a class annotation to a Ruby class
• become_java! - "promotes" a Ruby class to be a Java class
• include - can be used to signal that this class implements a Java
    interface
Compiling JRuby
The JRuby distribution comes with jrubyc, a compiler that
can produce either Java class files, or Java source (run
jrubyc --help for details).
rvm jruby
echo "puts 'hello'" > hello.rb
jrubyc hello.rb
ls -l hello*
javap -v hello | less

Free code obfuscation: Since only the .class file is need to
run your app, you can withhold the source code from
your users and provide only the .class file.

The .class file can be decompiled, but will be difficult to
comprehend, since Java byte code is similar in concept to
assembler code.
Omitting the ‘J’ in JRuby

Normally, it is necessary to run JRuby commands (jruby,
jirb) with their distinctive names beginning with j.

rvm eliminates the need for this.
rvm jruby

which ruby
# /Users/kbennett/.rvm/rubies/jruby-1.6.5/bin/ruby

which jruby
# /Users/kbennett/.rvm/rubies/jruby-1.6.5/bin/jruby

ls -l /Users/kbennett/.rvm/rubies/jruby-1.6.5/bin
# lrwxr-xr-x   1 kbennett   staff   5 Jan 19 13:25 ruby -> jruby
Defaulting to 1.9

Most people will want JRuby to run in 1.9 mode.
To automate this, ensure that the environment variable
JRUBY_OPTS will contain --1.9.
Most developers will accomplish this by inserting the
following line in their shell startup script
(e.g. .bashrc, .zshrc):
export JRUBY_OPTS=--1.9

This can be overridden by eliminating or replacing that
environment variable’s value:
JRUBY_OPTS=     ruby ...
# or
export JRUBY_OPTS=
Nailgun
Nailgun is a tool packaged with JRuby that enables
sharing a single JRuby virtual machine instance by JRuby
scripts to eliminate the delay associated with JVM
startup.

#   If $JRUBY_OPTS contains “--1.9” so that JRuby runs
#   in 1.9 mode by default, then this must be overridden
#   for the Nailgun server to start.
#   See but at http://jira.codehaus.org/browse/JRUBY-5611.

# Start up the shared Nailgun server:
JRUBY_OPTS=   jruby –ng-server

time ruby -e "puts 123"
#123
#ruby -e "puts 123"   2.89s user 0.14s system 217% cpu 1.390 total

time ruby --ng -e "puts 123"
#123
#ruby --ng -e "puts 123"   0.00s user 0.00s system 0% cpu 0.488 total
Warbler

“Warbler is a gem that makes a .war file out of a Rails,
Merb, or Rack-based application. The intent is to provide
a minimal, flexible, ruby-like way to bundle all your
application files for deployment to a Java application
server.”

- http://kenai.com/projects/warbler/pages/Home

Similarly, you can combine jruby.jar and your Ruby code
into a single .jar file that can be executed simply and
automatically by the operating system.
JVisualVM
JVisualVM is a monitoring, profiling, and troubleshooting
tool packaged with the JDK (Java Development Kit).
Sample Multithreaded Program

num_threads = 5
threads = []

(0...num_threads).each do |n|
  threads << Thread.new do
    loop { puts "#{' ' * (5 * n)}#{n}n" }
  end
end

threads.each { |thread| thread.join }
Multithreading CPU Usage by Ruby Version

rvm 1.8
ruby multithread.rb

PID    COMMAND        %CPU TIME      #TH   #WQ   #POR #MREG RPRVT   RSHRD   RSIZE
367    Terminal       105.8 01:20.36 5/1   1     123- 166+ 267M+    43M     278M+
1104   ruby           82.7 00:13.40 1/1    0     18   28    1248K   240K    2108K

rvm 1.9
ruby multithread.rb

PID    COMMAND        %CPU TIME      #TH   #WQ   #POR #MREG RPRVT RSHRD     RSIZE
1249   ruby           167.6 00:20.85 7/2   0     41   61    2972K+ 240K     4632K+
367    Terminal       153.0 03:49.55 5/1   1     125 267    716M+ 43M       772M+

# What happened?   I thought 1.9 used a GIL (Global Interpreter Lock)?

rvm jruby
ruby multithread.rb

ID   COMMAND       %CPU TIME      #TH #WQ        #POR #MREG RPRVT RSHRD RSIZE
1364 java           137.1 00:25.84 23/2 1         201 236    80M   3144K 93M
367   Terminal      120.8 05:05.62 6/1 2          127- 291   813M+ 43M   901M+
References

• JRuby Main Site: http://jruby.org
• JRuby Book: http://pragprog.com/book/jruby/using-jruby
• Charlie Nutter's JRuby Slide Show:
   http://www.slideshare.net/CharlesNutter/rubyconf-uruguay-2010-jruby

• JRuby as a Better Language for the JVM - http://krbtech.wordpress.com/
   2009/02/26/jruby-a-better-language-for-the-javavirtual-machine/

• Multilanguage Swing Github Repo:
   https://github.com/keithrbennett/multilanguage-swing

• This slideshow:
   http://www.slideshare.net/keithrbennett/jruby-synergyofrubyandjava

More Related Content

What's hot

Cooking Perl with Chef: Real World Tutorial with Jitterbug
Cooking Perl with Chef: Real World Tutorial with JitterbugCooking Perl with Chef: Real World Tutorial with Jitterbug
Cooking Perl with Chef: Real World Tutorial with Jitterbug
David Golden
 
Complex Made Simple: Sleep Better with TorqueBox
Complex Made Simple: Sleep Better with TorqueBoxComplex Made Simple: Sleep Better with TorqueBox
Complex Made Simple: Sleep Better with TorqueBox
bobmcwhirter
 
PECL Picks - Extensions to make your life better
PECL Picks - Extensions to make your life betterPECL Picks - Extensions to make your life better
PECL Picks - Extensions to make your life better
ZendCon
 
ZK_Arch_notes_20081121
ZK_Arch_notes_20081121ZK_Arch_notes_20081121
ZK_Arch_notes_20081121WANGCHOU LU
 
Ruby - a tester's best friend
Ruby - a tester's best friendRuby - a tester's best friend
Ruby - a tester's best friendPeter Lind
 
When Ruby Meets Java - The Power of Torquebox
When Ruby Meets Java - The Power of TorqueboxWhen Ruby Meets Java - The Power of Torquebox
When Ruby Meets Java - The Power of Torquebox
rockyjaiswal
 
Java/Spring과 Node.js의 공존 시즌2
Java/Spring과 Node.js의 공존 시즌2Java/Spring과 Node.js의 공존 시즌2
Java/Spring과 Node.js의 공존 시즌2
동수 장
 
Large-scaled Deploy Over 100 Servers in 3 Minutes
Large-scaled Deploy Over 100 Servers in 3 MinutesLarge-scaled Deploy Over 100 Servers in 3 Minutes
Large-scaled Deploy Over 100 Servers in 3 Minutes
Hiroshi SHIBATA
 
JUDCon 2010 Boston : BoxGrinder
JUDCon 2010 Boston : BoxGrinderJUDCon 2010 Boston : BoxGrinder
JUDCon 2010 Boston : BoxGrinder
marekgoldmann
 
Saint2012 mod process security
Saint2012 mod process securitySaint2012 mod process security
Saint2012 mod process security
Ryosuke MATSUMOTO
 
Find bottleneck and tuning in Java Application
Find bottleneck and tuning in Java ApplicationFind bottleneck and tuning in Java Application
Find bottleneck and tuning in Java Application
guest1f2740
 
Running Ruby on Solaris (RubyKaigi 2015, 12/Dec/2015)
Running Ruby on Solaris (RubyKaigi 2015, 12/Dec/2015)Running Ruby on Solaris (RubyKaigi 2015, 12/Dec/2015)
Running Ruby on Solaris (RubyKaigi 2015, 12/Dec/2015)
ngotogenome
 
Going to Mars with Groovy Domain-Specific Languages
Going to Mars with Groovy Domain-Specific LanguagesGoing to Mars with Groovy Domain-Specific Languages
Going to Mars with Groovy Domain-Specific Languages
Guillaume Laforge
 
"What's New in HotSpot JVM 8" @ JPoint 2014, Moscow, Russia
"What's New in HotSpot JVM 8" @ JPoint 2014, Moscow, Russia "What's New in HotSpot JVM 8" @ JPoint 2014, Moscow, Russia
"What's New in HotSpot JVM 8" @ JPoint 2014, Moscow, Russia Vladimir Ivanov
 
Middleware as Code with mruby
Middleware as Code with mrubyMiddleware as Code with mruby
Middleware as Code with mruby
Hiroshi SHIBATA
 
JUDCon 2010 Boston : TorqueBox
JUDCon 2010 Boston : TorqueBoxJUDCon 2010 Boston : TorqueBox
JUDCon 2010 Boston : TorqueBox
marekgoldmann
 
Debugging on rails
Debugging on railsDebugging on rails
Debugging on rails
Mykhaylo Sorochan
 
JRuby and You
JRuby and YouJRuby and You
JRuby and You
Hiro Asari
 

What's hot (19)

Cooking Perl with Chef: Real World Tutorial with Jitterbug
Cooking Perl with Chef: Real World Tutorial with JitterbugCooking Perl with Chef: Real World Tutorial with Jitterbug
Cooking Perl with Chef: Real World Tutorial with Jitterbug
 
Complex Made Simple: Sleep Better with TorqueBox
Complex Made Simple: Sleep Better with TorqueBoxComplex Made Simple: Sleep Better with TorqueBox
Complex Made Simple: Sleep Better with TorqueBox
 
PECL Picks - Extensions to make your life better
PECL Picks - Extensions to make your life betterPECL Picks - Extensions to make your life better
PECL Picks - Extensions to make your life better
 
ZK_Arch_notes_20081121
ZK_Arch_notes_20081121ZK_Arch_notes_20081121
ZK_Arch_notes_20081121
 
Ruby - a tester's best friend
Ruby - a tester's best friendRuby - a tester's best friend
Ruby - a tester's best friend
 
JRuby Basics
JRuby BasicsJRuby Basics
JRuby Basics
 
When Ruby Meets Java - The Power of Torquebox
When Ruby Meets Java - The Power of TorqueboxWhen Ruby Meets Java - The Power of Torquebox
When Ruby Meets Java - The Power of Torquebox
 
Java/Spring과 Node.js의 공존 시즌2
Java/Spring과 Node.js의 공존 시즌2Java/Spring과 Node.js의 공존 시즌2
Java/Spring과 Node.js의 공존 시즌2
 
Large-scaled Deploy Over 100 Servers in 3 Minutes
Large-scaled Deploy Over 100 Servers in 3 MinutesLarge-scaled Deploy Over 100 Servers in 3 Minutes
Large-scaled Deploy Over 100 Servers in 3 Minutes
 
JUDCon 2010 Boston : BoxGrinder
JUDCon 2010 Boston : BoxGrinderJUDCon 2010 Boston : BoxGrinder
JUDCon 2010 Boston : BoxGrinder
 
Saint2012 mod process security
Saint2012 mod process securitySaint2012 mod process security
Saint2012 mod process security
 
Find bottleneck and tuning in Java Application
Find bottleneck and tuning in Java ApplicationFind bottleneck and tuning in Java Application
Find bottleneck and tuning in Java Application
 
Running Ruby on Solaris (RubyKaigi 2015, 12/Dec/2015)
Running Ruby on Solaris (RubyKaigi 2015, 12/Dec/2015)Running Ruby on Solaris (RubyKaigi 2015, 12/Dec/2015)
Running Ruby on Solaris (RubyKaigi 2015, 12/Dec/2015)
 
Going to Mars with Groovy Domain-Specific Languages
Going to Mars with Groovy Domain-Specific LanguagesGoing to Mars with Groovy Domain-Specific Languages
Going to Mars with Groovy Domain-Specific Languages
 
"What's New in HotSpot JVM 8" @ JPoint 2014, Moscow, Russia
"What's New in HotSpot JVM 8" @ JPoint 2014, Moscow, Russia "What's New in HotSpot JVM 8" @ JPoint 2014, Moscow, Russia
"What's New in HotSpot JVM 8" @ JPoint 2014, Moscow, Russia
 
Middleware as Code with mruby
Middleware as Code with mrubyMiddleware as Code with mruby
Middleware as Code with mruby
 
JUDCon 2010 Boston : TorqueBox
JUDCon 2010 Boston : TorqueBoxJUDCon 2010 Boston : TorqueBox
JUDCon 2010 Boston : TorqueBox
 
Debugging on rails
Debugging on railsDebugging on rails
Debugging on rails
 
JRuby and You
JRuby and YouJRuby and You
JRuby and You
 

Viewers also liked

What I Love About Ruby
What I Love About RubyWhat I Love About Ruby
What I Love About Ruby
Keith Bennett
 
Unix Command Line Productivity Tips
Unix Command Line Productivity TipsUnix Command Line Productivity Tips
Unix Command Line Productivity Tips
Keith Bennett
 
Inaugural Addresses
Inaugural AddressesInaugural Addresses
Inaugural Addresses
Booz Allen Hamilton
 
Teaching Students with Emojis, Emoticons, & Textspeak
Teaching Students with Emojis, Emoticons, & TextspeakTeaching Students with Emojis, Emoticons, & Textspeak
Teaching Students with Emojis, Emoticons, & Textspeak
Shelly Sanchez Terrell
 
Study: The Future of VR, AR and Self-Driving Cars
Study: The Future of VR, AR and Self-Driving CarsStudy: The Future of VR, AR and Self-Driving Cars
Study: The Future of VR, AR and Self-Driving Cars
LinkedIn
 
Hype vs. Reality: The AI Explainer
Hype vs. Reality: The AI ExplainerHype vs. Reality: The AI Explainer
Hype vs. Reality: The AI Explainer
Luminary Labs
 

Viewers also liked (6)

What I Love About Ruby
What I Love About RubyWhat I Love About Ruby
What I Love About Ruby
 
Unix Command Line Productivity Tips
Unix Command Line Productivity TipsUnix Command Line Productivity Tips
Unix Command Line Productivity Tips
 
Inaugural Addresses
Inaugural AddressesInaugural Addresses
Inaugural Addresses
 
Teaching Students with Emojis, Emoticons, & Textspeak
Teaching Students with Emojis, Emoticons, & TextspeakTeaching Students with Emojis, Emoticons, & Textspeak
Teaching Students with Emojis, Emoticons, & Textspeak
 
Study: The Future of VR, AR and Self-Driving Cars
Study: The Future of VR, AR and Self-Driving CarsStudy: The Future of VR, AR and Self-Driving Cars
Study: The Future of VR, AR and Self-Driving Cars
 
Hype vs. Reality: The AI Explainer
Hype vs. Reality: The AI ExplainerHype vs. Reality: The AI Explainer
Hype vs. Reality: The AI Explainer
 

Similar to Jruby synergy-of-ruby-and-java

The Enterprise Strikes Back
The Enterprise Strikes BackThe Enterprise Strikes Back
The Enterprise Strikes Back
Burke Libbey
 
JRuby - Enterprise 2.0
JRuby - Enterprise 2.0JRuby - Enterprise 2.0
JRuby - Enterprise 2.0Jan Sifra
 
Практики применения JRuby
Практики применения JRubyПрактики применения JRuby
Практики применения JRuby
.toster
 
Getting Started with Rails on GlassFish (Hands-on Lab) - Spark IT 2010
Getting Started with Rails on GlassFish (Hands-on Lab) - Spark IT 2010Getting Started with Rails on GlassFish (Hands-on Lab) - Spark IT 2010
Getting Started with Rails on GlassFish (Hands-on Lab) - Spark IT 2010
Arun Gupta
 
Practical JRuby
Practical JRubyPractical JRuby
Practical JRuby
David Keener
 
Dynamic Languages Web Frameworks Indicthreads 2009
Dynamic Languages Web Frameworks Indicthreads 2009Dynamic Languages Web Frameworks Indicthreads 2009
Dynamic Languages Web Frameworks Indicthreads 2009
Arun Gupta
 
Ruby on Rails survival guide of an aged Java developer
Ruby on Rails survival guide of an aged Java developerRuby on Rails survival guide of an aged Java developer
Ruby on Rails survival guide of an aged Java developer
gicappa
 
Jaoo Michael Neale 09
Jaoo Michael Neale 09Jaoo Michael Neale 09
Jaoo Michael Neale 09
Michael Neale
 
JRuby, Ruby, Rails and You on the Cloud
JRuby, Ruby, Rails and You on the CloudJRuby, Ruby, Rails and You on the Cloud
JRuby, Ruby, Rails and You on the Cloud
Hiro Asari
 
Day 8 - jRuby
Day 8 - jRubyDay 8 - jRuby
Day 8 - jRuby
Barry Jones
 
Introduction to JIB and Google Cloud Run
Introduction to JIB and Google Cloud RunIntroduction to JIB and Google Cloud Run
Introduction to JIB and Google Cloud Run
Saiyam Pathak
 
Leaner microservices with Java 10
Leaner microservices with Java 10Leaner microservices with Java 10
Leaner microservices with Java 10
Arto Santala
 
Building web framework with Rack
Building web framework with RackBuilding web framework with Rack
Building web framework with Rack
sickill
 
JavaScript Growing Up
JavaScript Growing UpJavaScript Growing Up
JavaScript Growing UpDavid Padbury
 
JRuby + Rails = Awesome Java Web Framework at Jfokus 2011
JRuby + Rails = Awesome Java Web Framework at Jfokus 2011JRuby + Rails = Awesome Java Web Framework at Jfokus 2011
JRuby + Rails = Awesome Java Web Framework at Jfokus 2011Nick Sieger
 
Polygot Java EE on the GraalVM
Polygot Java EE on the GraalVMPolygot Java EE on the GraalVM
Polygot Java EE on the GraalVM
Ryan Cuprak
 
Java 8 Overview
Java 8 OverviewJava 8 Overview
Java 8 Overview
Nicola Pedot
 

Similar to Jruby synergy-of-ruby-and-java (20)

Ugo Cei Presentation
Ugo Cei PresentationUgo Cei Presentation
Ugo Cei Presentation
 
The Enterprise Strikes Back
The Enterprise Strikes BackThe Enterprise Strikes Back
The Enterprise Strikes Back
 
JRuby - Enterprise 2.0
JRuby - Enterprise 2.0JRuby - Enterprise 2.0
JRuby - Enterprise 2.0
 
Практики применения JRuby
Практики применения JRubyПрактики применения JRuby
Практики применения JRuby
 
Getting Started with Rails on GlassFish (Hands-on Lab) - Spark IT 2010
Getting Started with Rails on GlassFish (Hands-on Lab) - Spark IT 2010Getting Started with Rails on GlassFish (Hands-on Lab) - Spark IT 2010
Getting Started with Rails on GlassFish (Hands-on Lab) - Spark IT 2010
 
Practical JRuby
Practical JRubyPractical JRuby
Practical JRuby
 
Dynamic Languages Web Frameworks Indicthreads 2009
Dynamic Languages Web Frameworks Indicthreads 2009Dynamic Languages Web Frameworks Indicthreads 2009
Dynamic Languages Web Frameworks Indicthreads 2009
 
Ruby on Rails survival guide of an aged Java developer
Ruby on Rails survival guide of an aged Java developerRuby on Rails survival guide of an aged Java developer
Ruby on Rails survival guide of an aged Java developer
 
Jaoo Michael Neale 09
Jaoo Michael Neale 09Jaoo Michael Neale 09
Jaoo Michael Neale 09
 
JRuby, Ruby, Rails and You on the Cloud
JRuby, Ruby, Rails and You on the CloudJRuby, Ruby, Rails and You on the Cloud
JRuby, Ruby, Rails and You on the Cloud
 
Day 8 - jRuby
Day 8 - jRubyDay 8 - jRuby
Day 8 - jRuby
 
Introduction to JIB and Google Cloud Run
Introduction to JIB and Google Cloud RunIntroduction to JIB and Google Cloud Run
Introduction to JIB and Google Cloud Run
 
Leaner microservices with Java 10
Leaner microservices with Java 10Leaner microservices with Java 10
Leaner microservices with Java 10
 
Building web framework with Rack
Building web framework with RackBuilding web framework with Rack
Building web framework with Rack
 
JavaScript Growing Up
JavaScript Growing UpJavaScript Growing Up
JavaScript Growing Up
 
JRuby + Rails = Awesome Java Web Framework at Jfokus 2011
JRuby + Rails = Awesome Java Web Framework at Jfokus 2011JRuby + Rails = Awesome Java Web Framework at Jfokus 2011
JRuby + Rails = Awesome Java Web Framework at Jfokus 2011
 
Cooking with Chef
Cooking with ChefCooking with Chef
Cooking with Chef
 
Polygot Java EE on the GraalVM
Polygot Java EE on the GraalVMPolygot Java EE on the GraalVM
Polygot Java EE on the GraalVM
 
Java 8 Overview
Java 8 OverviewJava 8 Overview
Java 8 Overview
 
First Day With J Ruby
First Day With J RubyFirst Day With J Ruby
First Day With J Ruby
 

Recently uploaded

Communications Mining Series - Zero to Hero - Session 1
Communications Mining Series - Zero to Hero - Session 1Communications Mining Series - Zero to Hero - Session 1
Communications Mining Series - Zero to Hero - Session 1
DianaGray10
 
Essentials of Automations: The Art of Triggers and Actions in FME
Essentials of Automations: The Art of Triggers and Actions in FMEEssentials of Automations: The Art of Triggers and Actions in FME
Essentials of Automations: The Art of Triggers and Actions in FME
Safe Software
 
Pushing the limits of ePRTC: 100ns holdover for 100 days
Pushing the limits of ePRTC: 100ns holdover for 100 daysPushing the limits of ePRTC: 100ns holdover for 100 days
Pushing the limits of ePRTC: 100ns holdover for 100 days
Adtran
 
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdfFIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
FIDO Alliance
 
Elevating Tactical DDD Patterns Through Object Calisthenics
Elevating Tactical DDD Patterns Through Object CalisthenicsElevating Tactical DDD Patterns Through Object Calisthenics
Elevating Tactical DDD Patterns Through Object Calisthenics
Dorra BARTAGUIZ
 
Securing your Kubernetes cluster_ a step-by-step guide to success !
Securing your Kubernetes cluster_ a step-by-step guide to success !Securing your Kubernetes cluster_ a step-by-step guide to success !
Securing your Kubernetes cluster_ a step-by-step guide to success !
KatiaHIMEUR1
 
Free Complete Python - A step towards Data Science
Free Complete Python - A step towards Data ScienceFree Complete Python - A step towards Data Science
Free Complete Python - A step towards Data Science
RinaMondal9
 
The Art of the Pitch: WordPress Relationships and Sales
The Art of the Pitch: WordPress Relationships and SalesThe Art of the Pitch: WordPress Relationships and Sales
The Art of the Pitch: WordPress Relationships and Sales
Laura Byrne
 
Climate Impact of Software Testing at Nordic Testing Days
Climate Impact of Software Testing at Nordic Testing DaysClimate Impact of Software Testing at Nordic Testing Days
Climate Impact of Software Testing at Nordic Testing Days
Kari Kakkonen
 
UiPath Test Automation using UiPath Test Suite series, part 5
UiPath Test Automation using UiPath Test Suite series, part 5UiPath Test Automation using UiPath Test Suite series, part 5
UiPath Test Automation using UiPath Test Suite series, part 5
DianaGray10
 
Monitoring Java Application Security with JDK Tools and JFR Events
Monitoring Java Application Security with JDK Tools and JFR EventsMonitoring Java Application Security with JDK Tools and JFR Events
Monitoring Java Application Security with JDK Tools and JFR Events
Ana-Maria Mihalceanu
 
FIDO Alliance Osaka Seminar: FIDO Security Aspects.pdf
FIDO Alliance Osaka Seminar: FIDO Security Aspects.pdfFIDO Alliance Osaka Seminar: FIDO Security Aspects.pdf
FIDO Alliance Osaka Seminar: FIDO Security Aspects.pdf
FIDO Alliance
 
GraphSummit Singapore | Graphing Success: Revolutionising Organisational Stru...
GraphSummit Singapore | Graphing Success: Revolutionising Organisational Stru...GraphSummit Singapore | Graphing Success: Revolutionising Organisational Stru...
GraphSummit Singapore | Graphing Success: Revolutionising Organisational Stru...
Neo4j
 
Introduction to CHERI technology - Cybersecurity
Introduction to CHERI technology - CybersecurityIntroduction to CHERI technology - Cybersecurity
Introduction to CHERI technology - Cybersecurity
mikeeftimakis1
 
Observability Concepts EVERY Developer Should Know -- DeveloperWeek Europe.pdf
Observability Concepts EVERY Developer Should Know -- DeveloperWeek Europe.pdfObservability Concepts EVERY Developer Should Know -- DeveloperWeek Europe.pdf
Observability Concepts EVERY Developer Should Know -- DeveloperWeek Europe.pdf
Paige Cruz
 
PCI PIN Basics Webinar from the Controlcase Team
PCI PIN Basics Webinar from the Controlcase TeamPCI PIN Basics Webinar from the Controlcase Team
PCI PIN Basics Webinar from the Controlcase Team
ControlCase
 
FIDO Alliance Osaka Seminar: Passkeys at Amazon.pdf
FIDO Alliance Osaka Seminar: Passkeys at Amazon.pdfFIDO Alliance Osaka Seminar: Passkeys at Amazon.pdf
FIDO Alliance Osaka Seminar: Passkeys at Amazon.pdf
FIDO Alliance
 
Transcript: Selling digital books in 2024: Insights from industry leaders - T...
Transcript: Selling digital books in 2024: Insights from industry leaders - T...Transcript: Selling digital books in 2024: Insights from industry leaders - T...
Transcript: Selling digital books in 2024: Insights from industry leaders - T...
BookNet Canada
 
Why You Should Replace Windows 11 with Nitrux Linux 3.5.0 for enhanced perfor...
Why You Should Replace Windows 11 with Nitrux Linux 3.5.0 for enhanced perfor...Why You Should Replace Windows 11 with Nitrux Linux 3.5.0 for enhanced perfor...
Why You Should Replace Windows 11 with Nitrux Linux 3.5.0 for enhanced perfor...
SOFTTECHHUB
 
State of ICS and IoT Cyber Threat Landscape Report 2024 preview
State of ICS and IoT Cyber Threat Landscape Report 2024 previewState of ICS and IoT Cyber Threat Landscape Report 2024 preview
State of ICS and IoT Cyber Threat Landscape Report 2024 preview
Prayukth K V
 

Recently uploaded (20)

Communications Mining Series - Zero to Hero - Session 1
Communications Mining Series - Zero to Hero - Session 1Communications Mining Series - Zero to Hero - Session 1
Communications Mining Series - Zero to Hero - Session 1
 
Essentials of Automations: The Art of Triggers and Actions in FME
Essentials of Automations: The Art of Triggers and Actions in FMEEssentials of Automations: The Art of Triggers and Actions in FME
Essentials of Automations: The Art of Triggers and Actions in FME
 
Pushing the limits of ePRTC: 100ns holdover for 100 days
Pushing the limits of ePRTC: 100ns holdover for 100 daysPushing the limits of ePRTC: 100ns holdover for 100 days
Pushing the limits of ePRTC: 100ns holdover for 100 days
 
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdfFIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
 
Elevating Tactical DDD Patterns Through Object Calisthenics
Elevating Tactical DDD Patterns Through Object CalisthenicsElevating Tactical DDD Patterns Through Object Calisthenics
Elevating Tactical DDD Patterns Through Object Calisthenics
 
Securing your Kubernetes cluster_ a step-by-step guide to success !
Securing your Kubernetes cluster_ a step-by-step guide to success !Securing your Kubernetes cluster_ a step-by-step guide to success !
Securing your Kubernetes cluster_ a step-by-step guide to success !
 
Free Complete Python - A step towards Data Science
Free Complete Python - A step towards Data ScienceFree Complete Python - A step towards Data Science
Free Complete Python - A step towards Data Science
 
The Art of the Pitch: WordPress Relationships and Sales
The Art of the Pitch: WordPress Relationships and SalesThe Art of the Pitch: WordPress Relationships and Sales
The Art of the Pitch: WordPress Relationships and Sales
 
Climate Impact of Software Testing at Nordic Testing Days
Climate Impact of Software Testing at Nordic Testing DaysClimate Impact of Software Testing at Nordic Testing Days
Climate Impact of Software Testing at Nordic Testing Days
 
UiPath Test Automation using UiPath Test Suite series, part 5
UiPath Test Automation using UiPath Test Suite series, part 5UiPath Test Automation using UiPath Test Suite series, part 5
UiPath Test Automation using UiPath Test Suite series, part 5
 
Monitoring Java Application Security with JDK Tools and JFR Events
Monitoring Java Application Security with JDK Tools and JFR EventsMonitoring Java Application Security with JDK Tools and JFR Events
Monitoring Java Application Security with JDK Tools and JFR Events
 
FIDO Alliance Osaka Seminar: FIDO Security Aspects.pdf
FIDO Alliance Osaka Seminar: FIDO Security Aspects.pdfFIDO Alliance Osaka Seminar: FIDO Security Aspects.pdf
FIDO Alliance Osaka Seminar: FIDO Security Aspects.pdf
 
GraphSummit Singapore | Graphing Success: Revolutionising Organisational Stru...
GraphSummit Singapore | Graphing Success: Revolutionising Organisational Stru...GraphSummit Singapore | Graphing Success: Revolutionising Organisational Stru...
GraphSummit Singapore | Graphing Success: Revolutionising Organisational Stru...
 
Introduction to CHERI technology - Cybersecurity
Introduction to CHERI technology - CybersecurityIntroduction to CHERI technology - Cybersecurity
Introduction to CHERI technology - Cybersecurity
 
Observability Concepts EVERY Developer Should Know -- DeveloperWeek Europe.pdf
Observability Concepts EVERY Developer Should Know -- DeveloperWeek Europe.pdfObservability Concepts EVERY Developer Should Know -- DeveloperWeek Europe.pdf
Observability Concepts EVERY Developer Should Know -- DeveloperWeek Europe.pdf
 
PCI PIN Basics Webinar from the Controlcase Team
PCI PIN Basics Webinar from the Controlcase TeamPCI PIN Basics Webinar from the Controlcase Team
PCI PIN Basics Webinar from the Controlcase Team
 
FIDO Alliance Osaka Seminar: Passkeys at Amazon.pdf
FIDO Alliance Osaka Seminar: Passkeys at Amazon.pdfFIDO Alliance Osaka Seminar: Passkeys at Amazon.pdf
FIDO Alliance Osaka Seminar: Passkeys at Amazon.pdf
 
Transcript: Selling digital books in 2024: Insights from industry leaders - T...
Transcript: Selling digital books in 2024: Insights from industry leaders - T...Transcript: Selling digital books in 2024: Insights from industry leaders - T...
Transcript: Selling digital books in 2024: Insights from industry leaders - T...
 
Why You Should Replace Windows 11 with Nitrux Linux 3.5.0 for enhanced perfor...
Why You Should Replace Windows 11 with Nitrux Linux 3.5.0 for enhanced perfor...Why You Should Replace Windows 11 with Nitrux Linux 3.5.0 for enhanced perfor...
Why You Should Replace Windows 11 with Nitrux Linux 3.5.0 for enhanced perfor...
 
State of ICS and IoT Cyber Threat Landscape Report 2024 preview
State of ICS and IoT Cyber Threat Landscape Report 2024 previewState of ICS and IoT Cyber Threat Landscape Report 2024 preview
State of ICS and IoT Cyber Threat Landscape Report 2024 preview
 

Jruby synergy-of-ruby-and-java

  • 1. JRuby -- Java/Ruby Synergy Keith Bennett keithrbennett --at-- gmail.com @keithrbennett
  • 2. What is JRuby? An implementation of the Ruby language on the Java Virtual Machine: • Open Source • Main site at http://jruby.org/ • Source Code at https://github.com/jruby/jruby • Matz writes C so we don't have to; the JRuby team writes Java so we don't have to. • Both1.8 and 1.9 compatible.
  • 3. Non-Java Languages on the JVM "The Java virtual machine knows nothing of the Java programming language, only of a particular binary format, the class file format." - from http://java.sun.com/developer/technicalArticles/DynTypeLang/ • JRuby • Scala • Clojure • Groovy • JavaScript
  • 4. Technical Benefits of Running Ruby on the JVM • mature and stable platform • more portable – runs on older and obscure OS's that have JVM's • access to a huge number of libraries, e.g. XML, SOAP • excellent performance, garbage collection, and multithreading • performance tuning and profiling tools (e.g. jvisualvm) • excellent I18N (internationalization) support • highly scalable • can drive Java API's
  • 5. Benefits of Ruby on the JVM for Enterprise Java Shops • all the technical benefits previously mentioned, plus: • can use Ruby DB tools such as Active Record (esp. useful for migrations), DBI, etc. • can introduce development staff to scripting languages in general, and Ruby in particular • can automate one-off tasks that were previously too cost prohibitive to automate • can grow Ruby expertise that can be applied to various kinds of tasks (e.g. testing, scripting, web development) • can introduce it in a way that will not be distributed with production code • can use it for saner and more rapid development of Swing or SWT client-side GUI apps (see JRuby as a Better Language for the JVM - http://krbtech.wordpress.com/2009/02/26/jruby-a- better-language-for-the-javavirtual-machine/
  • 6. Benefits of Using JRuby for Ruby Developers • all the technical benefits previously mentioned, plus: • enables expanding customer base to include Java shops, many of which are very large and well funded. • enables creation of better solutions by increasing the set of parts that can be assembled into a solution -- sometimes Java is better (e.g. XML, SOAP support).
  • 7. Calling Java from Ruby require 'java' java.lang.System.properties.sort.each do |k,v| printf("%-30s %sn", k, v) } end displays: awt.nativeDoubleBuffering true awt.toolkit apple.awt.CToolkit file.encoding UTF-8 file.encoding.pkg sun.io file.separator / gopherProxySet false java.awt.printerjob apple.awt.CPrinterJob java.class.path java.class.version 50.0 …
  • 8. Adding Ruby methods to Java Classes require 'java' java_import java.lang.System class System def self.properties_pp s = '' properties.sort.each do |k,v| s << sprintf("%-34s %sn", k, v) end s end end puts System.properties_pp
  • 9. JRuby Method Generation JRuby generates snake-cased named methods for Java camel-case named methods, and conventional reader and writer methods à la attr_accessor: # The java.util.Locale class contains only getDefault and setDefault. # JRuby adds the others: jruby-1.6.5 :003 > puts Locale.methods.sort.grep /[Dd]efault/ default default= getDefault get_default setDefault set_default Also, JRuby makes the Enumerable interface available to some kinds of Java objects, enabling the above, and the following: Locale.iSOCountries.each { |country_code| puts country_code } Locale.iSOLanguages.each { |language_code| puts language_code }
  • 10. JRuby to Java Type Conversions jruby-1.6.5 :027 > 123.class => Fixnum jruby-1.6.5 :028 > 123.to_java.class => Java::JavaLang::Long
  • 11. Calling JRuby from Java import org.jruby.embed.ScriptingContainer; public class ScriptingContainerExample { public static void main(String[] args) { ScriptingContainer container = new ScriptingContainer(); container.put("$greeting", "Hello from JRuby!"); // optional // container.setLoadPaths(aJavaListOfDirectories); // optional container.setCompatVersion(org.jruby.CompatVersion.RUBY1_9); // optional container.runScriptlet("puts $greeting"); } } // >javac -cp ".:$JRUBY_JAR" ScriptingContainerExample.java // >java -cp ".:$JRUBY_JAR" ScriptingContainerExample // Hello from JRuby!
  • 12. Special Java Support Calls • java_import - Imports java classes • java_send - for specifying which function to call, by signature • java_alias - for creating an alias for a Java function with signature • java_method - for creating a callable reference to a Java function with signature • field_accessor - for accessing Java instance variables, even private ones • add_class_annotation - Adds a class annotation to a Ruby class • become_java! - "promotes" a Ruby class to be a Java class • include - can be used to signal that this class implements a Java interface
  • 13. Compiling JRuby The JRuby distribution comes with jrubyc, a compiler that can produce either Java class files, or Java source (run jrubyc --help for details). rvm jruby echo "puts 'hello'" > hello.rb jrubyc hello.rb ls -l hello* javap -v hello | less Free code obfuscation: Since only the .class file is need to run your app, you can withhold the source code from your users and provide only the .class file. The .class file can be decompiled, but will be difficult to comprehend, since Java byte code is similar in concept to assembler code.
  • 14. Omitting the ‘J’ in JRuby Normally, it is necessary to run JRuby commands (jruby, jirb) with their distinctive names beginning with j. rvm eliminates the need for this. rvm jruby which ruby # /Users/kbennett/.rvm/rubies/jruby-1.6.5/bin/ruby which jruby # /Users/kbennett/.rvm/rubies/jruby-1.6.5/bin/jruby ls -l /Users/kbennett/.rvm/rubies/jruby-1.6.5/bin # lrwxr-xr-x 1 kbennett staff 5 Jan 19 13:25 ruby -> jruby
  • 15. Defaulting to 1.9 Most people will want JRuby to run in 1.9 mode. To automate this, ensure that the environment variable JRUBY_OPTS will contain --1.9. Most developers will accomplish this by inserting the following line in their shell startup script (e.g. .bashrc, .zshrc): export JRUBY_OPTS=--1.9 This can be overridden by eliminating or replacing that environment variable’s value: JRUBY_OPTS= ruby ... # or export JRUBY_OPTS=
  • 16. Nailgun Nailgun is a tool packaged with JRuby that enables sharing a single JRuby virtual machine instance by JRuby scripts to eliminate the delay associated with JVM startup. # If $JRUBY_OPTS contains “--1.9” so that JRuby runs # in 1.9 mode by default, then this must be overridden # for the Nailgun server to start. # See but at http://jira.codehaus.org/browse/JRUBY-5611. # Start up the shared Nailgun server: JRUBY_OPTS= jruby –ng-server time ruby -e "puts 123" #123 #ruby -e "puts 123" 2.89s user 0.14s system 217% cpu 1.390 total time ruby --ng -e "puts 123" #123 #ruby --ng -e "puts 123" 0.00s user 0.00s system 0% cpu 0.488 total
  • 17. Warbler “Warbler is a gem that makes a .war file out of a Rails, Merb, or Rack-based application. The intent is to provide a minimal, flexible, ruby-like way to bundle all your application files for deployment to a Java application server.” - http://kenai.com/projects/warbler/pages/Home Similarly, you can combine jruby.jar and your Ruby code into a single .jar file that can be executed simply and automatically by the operating system.
  • 18. JVisualVM JVisualVM is a monitoring, profiling, and troubleshooting tool packaged with the JDK (Java Development Kit).
  • 19. Sample Multithreaded Program num_threads = 5 threads = [] (0...num_threads).each do |n| threads << Thread.new do loop { puts "#{' ' * (5 * n)}#{n}n" } end end threads.each { |thread| thread.join }
  • 20. Multithreading CPU Usage by Ruby Version rvm 1.8 ruby multithread.rb PID COMMAND %CPU TIME #TH #WQ #POR #MREG RPRVT RSHRD RSIZE 367 Terminal 105.8 01:20.36 5/1 1 123- 166+ 267M+ 43M 278M+ 1104 ruby 82.7 00:13.40 1/1 0 18 28 1248K 240K 2108K rvm 1.9 ruby multithread.rb PID COMMAND %CPU TIME #TH #WQ #POR #MREG RPRVT RSHRD RSIZE 1249 ruby 167.6 00:20.85 7/2 0 41 61 2972K+ 240K 4632K+ 367 Terminal 153.0 03:49.55 5/1 1 125 267 716M+ 43M 772M+ # What happened? I thought 1.9 used a GIL (Global Interpreter Lock)? rvm jruby ruby multithread.rb ID COMMAND %CPU TIME #TH #WQ #POR #MREG RPRVT RSHRD RSIZE 1364 java 137.1 00:25.84 23/2 1 201 236 80M 3144K 93M 367 Terminal 120.8 05:05.62 6/1 2 127- 291 813M+ 43M 901M+
  • 21. References • JRuby Main Site: http://jruby.org • JRuby Book: http://pragprog.com/book/jruby/using-jruby • Charlie Nutter's JRuby Slide Show: http://www.slideshare.net/CharlesNutter/rubyconf-uruguay-2010-jruby • JRuby as a Better Language for the JVM - http://krbtech.wordpress.com/ 2009/02/26/jruby-a-better-language-for-the-javavirtual-machine/ • Multilanguage Swing Github Repo: https://github.com/keithrbennett/multilanguage-swing • This slideshow: http://www.slideshare.net/keithrbennett/jruby-synergyofrubyandjava

Editor's Notes

  1. \n
  2. \n
  3. \n
  4. \n
  5. \n
  6. \n
  7. \n
  8. \n
  9. \n
  10. \n
  11. \n
  12. \n
  13. \n
  14. \n
  15. \n
  16. \n
  17. \n
  18. \n
  19. \n
  20. \n
  21. \n