How do I run a Sinatra app using JRuby?

How do I run a Sinatra app using JRuby?

RubyLearning is conducting a free, online JRuby 101 course – the first of its kind, on Google+ Some participants wanted an answer to their question “How do I run a Sinatra app using JRuby?” This blog post explains the same. Read on.

Pre-requisite

I have a Windows XP box but the following should work on Mac and Linux-based computers too.

Ensure that you have already installed JDK 6, JRuby and set the relevant system environment variables path, classpath, JAVA_HOME and JRUBY_HOME.

Install Bundler

Bundler helps prevent conflicting or missing gems and shines when it’s time to configure those dependencies at install time and runtime.

JRuby comes with a fairly loaded standard library from scratch but that does not mean there aren’t other things you’ll need. Almost all of them are installable as Gems. RubyGems is the premier package management tool for Ruby. It works fine with JRuby and JRuby ships with it. You use it through the gem command. We will need to run the JRuby version of the gem command and to ensure that, we use the -S flag to the interpreter.

Create a project folder (say c:\jrubysinatra) on your hard-disk. Ensure that your internet connection is active. Now, open a command window in this project folder and type:

jruby -S gem install bundler

Note: This approach (jruby -S) works for any Ruby command-line tool, including gem, rake, spec, and others.

Create a Gemfile

Next, in your project folder, create a Gemfile. It looks something like this:

source "http://rubygems.org"
gem 'sinatra'

This Gemfile says a few things. First, it says that bundler should look for gems declared in the Gemfile at http://rubygems.org. You can declare multiple Rubygems sources, and bundler will look for gems in the order you declared the sources. Next, you will have to list all your applications dependencies in there. Sinatra’s direct dependencies (Rack and Tilt) will, however, be automatically fetched and added by Bundler.

To make bundler install the dependencies, in the already open command window, type:

jruby -S bundle install

Because all the gems in your Gemfile have dependencies of their own (and some of those have their own dependencies), running jruby -S bundle install on the Gemfile above, will install quite a few gems. If any of the needed gems are already installed, Bundler will use them. After installing any needed gems to your system, bundler writes a snapshot of all the gems and versions that it installed to Gemfile.lock.

Write your Sinatra app

Create the file hellojruby.rb in the folder c:\jrubysinatra.

require "rubygems"
require "bundler/setup"

require "sinatra"

get '/hi' do
    "Hello JRuby World!"
end

Set up your Sinatra application to use Bundler

For your Sinatra application, you will need to set up bundler before trying to require any gems. At the top of the first file that your application loads (for Sinatra, the file that calls require "sinatra"), put the following code:

require "rubygems"
require "bundler/setup"

This will automatically discover your Gemfile, and make all the gems in your Gemfile available to Ruby (in technical terms, it puts the gems “on the load path”). You can think of it as an adding some extra powers to require “rubygems”.

Now that your code is available to Ruby, you can require the gems that you need. For instance, you can require "sinatra".

Run your Sinatra application

In the already open command window, type:

jruby -S bundle exec jruby hellojruby.rb

In the command window, you will see:

== Sinatra/1.2.6 has taken the stage on 4567 for development with backup from WEBrick
[2011-09-03 07:21:17] INFO  WEBrick 1.3.1
[2011-09-03 07:21:17] INFO  ruby 1.8.7 (2011-08-23) [java]
[2011-09-03 07:21:17] INFO  WEBrick::HTTPServer#start: pid=5128 port=4567

Access the Sinatra application

In your browser, visit the URL: http://localhost:4567/hi – the browser shall display “Hello JRuby World!

That’s it for now.

Feel free to ask questions and give feedback in the comments section of this post. Fellow Rubyists, if you would like to write a guest blog post for RubyLearning email me at satish [at] rubylearning.org

comments powered by Disqus