JRuby is essentially the Ruby programming language that is implemented in Java and it runs on the Java Virtual Machine. JRuby and the normal Ruby mostly have the same syntax so we can write normal Ruby code in JRuby without issue. JRuby has built-in support for a lot of gems that are commonly used in normal Ruby such as Rails, RSpec, Rake, and RubyGems. It also embeds an FFI subsystem to allow the use of C libraries bundled as gems. In addition, JRuby allows launching the Interactive Ruby Shell (irb) as Ruby MRI does. JRuby can run the Java Library as well as the C Library which the normal Ruby uses.
There are some advantages of using JRuby over the normal Ruby and one of them is the speed of execution thanks to the better computing power of Java Virtual Machine. Threading and concurrency are also big factors that make JRuby appealing to use. In normal Ruby, threading and concurrency are not really the focus when the language was originally built.
In addition to the above limitations, Global Interpreter Lock is also a factor that is hindering the speed of execution as well as performance for normal Ruby. JRuby on the other hand has real threading and concurrency that supports parallel execution which greatly improves efficiency and performance. Multiple developers can work at the same time without jeopardizing each other's work during the development process. Therefore you can run more Ruby processes with multiple threads for each process on a single server.
How To Set Up JRuby On Your Machine for macOS
If you already have Homebrew installed on you machine, all you need to do is to type the below code in the terminal to install JRuby:
$ brew install jruby
Otherwise see below for instructions:
- Have the latest version of Java SE installed on your machine.
- Go to JRuby Downloads page(https://www.jruby.org/download) and choose the "Binary" package in either .tar.gz or .zip format.
- Extract it's contents to a jruby directory under your home directory.
Put the bin directory on your $PATH. You can do this by running the below command:
$ export PATH=~/jruby/bin:$PATH
$ jruby --version
in your command to see if JRuby is installed successfully
Running rake, gem, rails, and more:
$ jruby -S gem list --local
$ jruby -S gem install rails activerecord-jdbcmysql-adapter
$ jruby -S rails new
$ cd blog
$ jruby -S rake -T
$ jruby -S rake db:create
$ jruby -S rake db:migrate
Running a Ruby Program
Below is a example of how to run a Ruby program using JRuby:
$ jruby rails server
$ jruby ruby_script.rb
One powerful feature of JRuby is its ability to invoke the classes of the Java Platform. To do this, one must first load JRuby's Java support, by calling "require 'java'". The following example creates a Java JFrame with a JLabel:
frame = javax.swing.Jframe.new
JRuby is a Ruby programming language that is tightly integrated with Java. Thanks to this relationship, JRuby has access to the powerful and vast Java libraries that developers can utilize when building applications. The processing power is also much stronger and faster which makes it overall an appealing option to use. Although, there are also downsides to it such as more time consuming to warm up, memory intensive, and lack of C Library compatibility. These tradeoffs have to be considered when determining the best approach but the integrated support of Java definitely makes JRuby a solid option to work with.