Focused

JRuby; The Eloquent Liaison

Java, Ruby and many other widely used programming languages all have —at least, the following two things in common —substantial bad press, and prophecies of doom. Yet their adoption and use does not plummet. Some of you might find this paradox baffling, why do we continue to use something that is unsuitable? The answer is truly quite simple —Human Subjectivity, and it’s been known to keep suitable and unsuitable things in use as-well as toss suitable and unsuitable things out.

A business man I knew expressed it best, this man owned two stores in the same town that sold the same products at pretty much the same prices. He enjoyed commerce and was a hands-on type of fellow whom you would often find managing one of the stores —but was positively never at the other, a manager in his employ would take care of that one, and few of the townsfolk ever knew that both stores were owned by the same person. When I eventually got to know him well enough, I asked why two stores to sell the same goods, he said —and I’m paraphrasing here;

My customers are fickle —some might have had a bad experience, some a bad day and some are simply in need of change. Some customers will rant at one of my stores about the dismal state of the other —while the inverse is taking place at the other, other customers switch it around every few months. People respond to their experiences and not always in an appropriate or rational manner, so this is the only way I can keep everybody happy.

Besides the hype related factors we looked at in Why Ruby Matters, programmers are as susceptible to mis-attributing bad experiences to languages and platforms as we all are at mis-attributing bad experiences in general. To mitigate the effects of hype and subjectivity on our deductions, we’ll appraise both the Java and Ruby ecosystems solely on their track record of real-world use-cases.


The Java Ecosystem

What we have in the JVM (Java Virtual Machine) is one of the most mature and performant platforms available. It’s actively used to develop solutions across many spheres and for all industries. Desktop? Embedded? Mobile? Server-side? Web-App? —The JVM is almost always a suitable choice. A simple search for which companies use it and the ways in which it’s being used, will give you a pretty clear answer with respect to its reach.

Java (the programming language) is the original —but not the only JVM language, the ecosystem is home to a number of other languages (and their respective communities) that have risen to prominence in recent years, including Clojure, Groovy, Kotlin and Scala. As with biological ecosystems, diversity is an indicator of health, and in the case of the Java ecosystem, a lot of the wonderful work being done by each of its resident communities can be used across all languages on its platform.

Besides the obvious benefits of a mature and extensively used platform, the real value of an ecosystem’s diversity is the effect its residents have on one another and the ecosystem as a whole. The cross-pollination of ideas becomes easier and the ecosystem itself more refined and efficient in the process.


The Ruby Ecosystem

Searching for which companies use Ruby will yield a great many results, you’ll no doubt recognize many of them as this last decade’s successful start-ups as-well as many recent trend-setters.

Ruby (the programming language) is about as old as Java, its mass adoption however, happened much later and is in part owed to the quick rise in demand for developing quick-to-market web-apps. Ruby established itself as a productive dynamic language early on and a strong community formed around it as demand soared, consequently it’s ecosystem has continued to grow and flourish at a rapid pace.

In its current stage of evolution, the Ruby ecosystem is robust and extensive. It remains a favorite of start-ups for reasons including its philosophy, community and efficacy with regard to productivity.


What’s a JRuby

I enjoy working in Ruby, Java —not so much. I find I can express my thoughts more naturally through Ruby as-well as read the expressions of others more easily, this minimizes the cognitive overhead for me which is a direct factor of influence on both my productivity and my level of contentment.

Java has shed some boilerplate and ceremony in recent times and I’m certain it will continue to do so as the cross-pollination of ideas continues —but I still find working in Ruby to be more enjoyable. Considering the amount of time one spends working with code, there’s a certain impact on overall quality of life here.

There are however a few things on the JVM that I miss when working with Ruby; performance, parallelism, certain libraries and reach. For some projects these things matter, and would be a significant enough factor for me to consider one of the alternative languages on the JVM, except.. this is one of those rare occasions in life where one gets to have their cake and eat it.

Yes, JRuby is a complete implementation of Ruby —on the JVM! Just like the other languages on the JVM you get to leverage all of the benefits of being on the platform, with the additional benefit of being able to use libraries from the Ruby ecosystem too.

Through JRuby we get to reap the benefits of Java’s mature ecosystem, along with the impressive innovation emerging from its diverse communities, as-well as the benefits of Ruby’s younger and more dynamic ecosystem —free to mix and match as we see fit.


Is JRuby a thing

JRuby has been around from before Ruby’s ascension to fame, it’s kept current by the duo of Charles Nutter and Thomas Enebo, with help from the community. The contributors —whom I owe a great debt of gratitude, are generally pretty lively on Github, #jruby and Gitter.

There are a few open source projects out there that make use of JRuby, but more than likely a greater amount of closed source products in enterprise settings that many are prohibited from commenting on publically. Look for wiki’s, blogs, talks and books on the topic if interested, if you can work with Ruby you can most definitely work with JRuby.


How to JRuby

We’re going to get a little crafty with this one and try get Java to do some async work for us utilizing the RxJava library. Here’s what you’re going to need to do:

  • Get JRuby set up as described > here
  • Pick a work directory and create yourself the hello_java.rb file
  • Get the RxJava jar (library) into your work directory from > here
#hello_java.rb
require "java"
require_relative "rxjava-1.3.3.jar"
java_import "java.util.concurrent.TimeUnit"
java_import "rx.Observable"
Observable.interval(1, TimeUnit::SECONDS)
.take(10)
.filter(->(i) { i.even? })
.subscribe(
->(n) { puts "Even: #{n}" },
->(e) { puts "Error encountered: #{e}" },
-> { puts "All Done." }
)
Observable.interval(3, TimeUnit::SECONDS)
.subscribe(->(_) { puts "¯\\_(ツ)_/¯" })
sleep(10)

Once we’ve required java and the RxJava jar downloaded earlier, we import the TimeUnit and Observable classes, then set up and subscribe to two Observable streams that will be running concurrently —the first of which will tick through every second but only output when the count is even, and the second will output every three seconds.

We’ll need a sleep at the end of this to keep the main thread alive, and we’re ready to fire a terminal up and run the script..

$ jruby -S hello_java.rb
Even: 0
Even: 2
¯\_(ツ)_/¯
Even: 4
¯\_(ツ)_/¯
Even: 6
Even: 8
¯\_(ツ)_/¯
All Done.

As the fine gentlemen and ladies of JRuby will tell you;

JRuby deals with Java so you don’t have to.

Share Comments