Fiona Tay
Why I like JRuby (and you should too)
Summarized using AI

Why I like JRuby (and you should too)

by Fiona Tay

In the presentation titled "Why I like JRuby (and you should too)" at LA RubyConf 2013, Fiona Tay, a Ruby and JavaScript developer, explores the benefits of using JRuby, a Ruby implementation on the Java Virtual Machine (JVM). While the title suggests a personal preference for JRuby, the talk centers around when to choose Ruby versus JRuby based on application requirements.

Key Points Discussed:
- Introduction to JRuby: JRuby allows developers to run Ruby applications on the JVM, making it a practical option in enterprise environments that commonly use Java.
- Compatibility and Setup: Transitioning a project from MRI (Matz's Ruby Implementation) to JRuby is straightforward, mainly requiring the installation of JRuby and adjusting configurations, especially if using RVM (Ruby Version Manager).
- Performance and Libraries:

- JRuby provides access to a wealth of Java libraries, such as the Stanford Parser for Natural Language Processing and JavaMail for advanced email functionalities.
- Deployments with JRuby can utilize Java infrastructure, such as packaging Ruby applications into WAR (Web Application Archive) files for easier deployment on traditional Java environments.
- Concurrency Benefits: Unlike MRI, JRuby supports true multithreading, allowing for better concurrency, which enhances performance in web servers and can reduce the need for additional hardware.
- Caveats of JRuby:

- Certain gems, especially those with C extensions, may not be compatible with JRuby, affecting functionality.
- With test-driven development in larger Rails projects, developers might experience longer startup times with JRuby, which could impact productivity.
- JRuby, while mature, is not as widely adopted as MRI, leading to potential compatibility issues with libraries primarily developed for MRI.

Examples and Case Studies:
- Fiona references Open Chorus, an open-source project with significant testing that benefits from using JRuby.
- She notes Twitter's initial use of MRI before transitioning to JRuby to cope with scalability challenges.

Conclusion and Takeaways:
- The choice between JRuby and MRI should depend on your application's specific needs. JRuby is advantageous for projects that can utilize Java libraries, require high performance, or are operating in Java-centric deployment environments. Conversely, simpler applications may not need the complexity of JRuby.

00:00:23.670 Okay, so next up is Fiona Tay. She's a Ruby and JavaScript developer for Pivotal Labs, based out of San Francisco. She's going to be talking to us about why JRuby is beneficial.
00:00:34.899 I'm really excited to be here because I actually used to live in the Inland Empire. Is anyone else here from the Inland Empire? Yes! Alright, so the first thing I want to clarify about my talk is that the schedule states it’s titled 'Why I like JRuby and you should too', but that’s not actually what my talk is about. My talk is actually about why I use Ruby sometimes and not other times.
00:01:20.350 Just for a quick introduction, JRuby is an implementation of Ruby on top of the JVM, which means that it runs on the Java Virtual Machine. In this presentation, I’ll discuss why you should consider using JRuby. This is from the perspective of someone like me who just wants to make web applications. I won’t be delving into compiler internals, as I assume that most of you don’t care about that. Instead, this talk will cover the good, the bad, and the ugly, much like the classic film.
00:01:36.369 A little about me: I’ve been doing Ruby for about three years and I mainly work with Rails. I have also been working with JRuby for the last six months on a project called Open Chorus. It’s an open source initiative that serves as a collaborative platform for data analysis—think of it as Google Docs for data scientists. As far as I know, we are one of the biggest JRuby projects out there, boasting over 10,000 tests. That’s quite a lot!
00:02:01.409 Now, let’s discuss more about JRuby. JRuby is essentially a Ruby implementation, and switching your project over from MRI to JRuby is quite straightforward. Step one is to install JRuby, and step two is to write some Ruby code. If you’re using RVM to manage your Ruby installations, it’s as simple as typing 'rvm use jruby'. If you’re not using RVM, I highly recommend starting to do so.
00:02:49.571 JRuby is, to an extent, interchangeable with other Ruby implementations. For instance, one plus one should be the same across all Ruby versions. If you find discrepancies, please let me know. The most popular implementation is MRI, also known as Matz’s Ruby Implementation, and it is essentially the default Ruby version. If you don’t know which Ruby you are using, you are probably using MRI.
00:03:03.720 The next most popular implementation is JRuby, which is what I’m talking about today. There are a few other alternatives out there, but, no offense meant to them, they are somewhat lesser-known. Now, moving on to how these Ruby implementations work: when you run Ruby code, it is interpreted by the respective interpreter. Nothing too rocket science about it.
00:03:58.300 In JRuby, the implementation of the interpreter is written using the Java Virtual Machine, which is where much of the power and benefits of JRuby come from. So, as I mentioned earlier, these implementations should work interchangeably, but that's not entirely true—some implementations can differ markedly. For example, one major difference lies in execution speed, library support, and existing bugs. While MRI is famously known as the reference implementation, it has its limitations.
00:05:16.470 JRuby has been around since 2002, which makes it quite mature. In fact, it is older than Ruby 1.9, the latest interpreter. JRuby has gained traction in real-world projects since 2005 and has been popularized in enterprise environments largely because Java is widely used in such settings.
00:05:48.919 You might not know this, but Twitter initially started as a Rails app running on MRI. After a few years, experiencing scaling issues, they transitioned to JRuby for a while; however, it seems they are not completely part of the Ruby community anymore, although they still run Ruby to some extent.
00:06:41.360 Now onto the good part: let's discuss the compelling reasons why you should consider using JRuby. The first major reason is access to Java libraries. There’s a wealth of enterprise software written in Java, and JRuby allows you to use these libraries seamlessly.
00:07:16.830 For those interested in Natural Language Processing (NLP), one prominent library is the Stanford Parser, which is written in Java. With JRuby, you can call it as if it were a Ruby library. Another example is JavaMail; while Rails has ActionMailer, it doesn’t offer fine-grained control for certain tasks. JavaMail, on the other hand, is a robust library that provides extensive functionality. JRuby facilitates a deep integration with Java code; you can directly invoke Java classes and methods as if they were Ruby classes.
00:08:27.580 Speaking of integrating Java code, here's a snippet that demonstrates how you can set up a desktop application using Java libraries via JRuby. For instance, if you are looking to use Swing for a desktop application, you can write code that looks very much like canonical Ruby while also gaining the benefits of Java’s libraries. This flexibility extends to using libraries written in other JVM-compatible languages such as Scala and Groovy.
00:09:12.920 JRuby allows you to leverage existing Java-based deployment infrastructure smoothly. You may find that tools like Warbler can help you compile your Ruby application into a single, deployable .war (Web Application Archive) file, which can be very useful if your deployment environment traditionally uses Java methods.
00:10:20.940 Using JRuby can contribute to your application’s performance as it benefits from the Java Virtual Machine's optimizations. Since JRuby operates as a Just-In-Time (JIT) compiler, your application actually speeds up the more it's left running, similar to how fine wine improves over time.
00:11:55.920 Additionally, you can tap into highly optimized Java libraries directly from JRuby. For example, if you need to parse XML using JRuby, it can be executed rapidly through Java’s XML parsing libraries which are known for their efficiency.
00:13:01.210 Now, let’s discuss some important caveats when it comes to using JRuby. Some gems, particularly those with C extensions, are not compatible with JRuby. This issue primarily arises due to how Ruby wraps these libraries differently based on the implementation.
00:13:32.120 Many of these gems are less maintained and therefore, the lack of availability might not be a major concern, but it is something worth considering. If you encounter gems that aren’t compliant, it may sometimes be a matter of changing dependencies or adjusting a few lines of code.
00:15:07.580 Moving forward, as a company that practices Test-Driven Development (TDD), we often face longer application startup times, especially in larger Rails projects; this is where JRuby can slow down developer productivity. Unfortunately, tools aimed at reducing startup times do not particularly work well with JRuby.
00:16:10.060 Another downside is that JRuby is a slightly less mature technology than MRI. While JRuby has matured a lot over the last few years and the core team is responsive, there are still compatibility issues due to libraries developed primarily on MRI.
00:17:07.260 Now, let’s talk about concurrency, which is what I consider 'the ugly.' Concurrency refers to multiple computations happening simultaneously. Unlike MRI, which uses a Global Interpreter Lock (GIL) restricting thread execution, JRuby supports true threading, enhancing concurrent programming.
00:18:07.540 Concurrency is a significant innovation, especially in the context of web server performance. You can utilize JRuby’s multithreading capabilities to potentially run multiple threads for your Ruby processes, enhancing performance without needing additional physical machines.
00:20:44.640 Ultimately, whether to switch to JRuby depends on your specific application needs. If your project can take advantage of Java libraries, needs high performance, or will be deployed within Java-based environments, JRuby could be beneficial. But if your application is relatively simple or has a predictable load, MRI may suffice.
00:22:43.620 Finally, there's a utility provided by the JRuby team that can help assess your project’s readiness for migration to JRuby by checking dependencies and code compatibility. So, thank you Pivotal Labs and the JRuby team for this opportunity.
00:23:06.420 I really appreciate it!
Explore all talks recorded at LA RubyConf 2013
+6