DEV Community

Danny Tseng
Danny Tseng

Posted on

Intro to JRuby

When I began my third module of the software engineering curriculum at the Flatiron school, we got introduced to the world of javascript and all the power this language possess to be used both as front-end and back-end language. I started wondering what's the difference between javascript and ruby which we have been using for the past two modules. As I wondered through the internet to try to look for information I am seeking, I came across this curious thing called "JRuby." Initially I thought the "J" stands for javascript, however, it turns out not to be the case. I ended up deviating from my original focus and started looking into what "JRuby" is all about.

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:

  1. Have the latest version of Java SE installed on your machine.
  2. Go to JRuby Downloads page(https://www.jruby.org/download) and choose the "Binary" package in either .tar.gz or .zip format.
  3. Extract it's contents to a jruby directory under your home directory.
  4. Put the bin directory on your $PATH. You can do this by running the below command:

              $ export PATH=~/jruby/bin:$PATH
    
  5. Run :

              $ 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:

require 'java'

frame = javax.swing.Jframe.new
frame.getContentPan.add
javax.swing.JLabel.new "Hi!"
frame.set_visible true

Conlusion

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.

Referrences:
https://en.wikipedia.org/wiki/JRuby
https://github.com/jruby/jruby/wiki
https://www.youtube.com/watch?v=Lja3HDcHNJA
https://en.wikipedia.org/wiki/Concurrency_(computer_science)
https://www.slideshare.net/FionaTay2/why-jruby-16743985/41-ConcurrencyJRuby_has_real_threadsMRI_Ruby
https://en.wikipedia.org/wiki/Thread_(computing)
https://www.youtube.com/watch?v=qJqC6xm-2PI
https://simple.wikipedia.org/wiki/Thread_(computer_science)

Top comments (0)