DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones AWS Cloud
by AWS Developer Relations
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones
AWS Cloud
by AWS Developer Relations
The Latest "Software Integration: The Intersection of APIs, Microservices, and Cloud-Based Systems" Trend Report
Get the report
  1. DZone
  2. Coding
  3. Frameworks
  4. How do I run a Sinatra app using JRuby?

How do I run a Sinatra app using JRuby?

Satish Talim user avatar by
Satish Talim
·
Nov. 14, 11 · Interview
Like (0)
Save
Tweet
Share
6.13K Views

Join the DZone community and get the full member experience.

Join For Free

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.

 

source:  http://rubylearning.com/blog/2011/09/03/how-do-i-run-a-sinatra-app-using-jruby/

Jruby Sinatra (software) app GEM (desktop environment) application

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Introduction To OpenSSH
  • What Are the Different Types of API Testing?
  • Create a CLI Chatbot With the ChatGPT API and Node.js
  • Journey to Event Driven, Part 1: Why Event-First Programming Changes Everything

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: