Friday, April 16, 2010

JRuby Steps 4 and 5

The JRuby 'Getting Started' page lists three steps:
1. Download JRuby: Visit the download page and grab the binaries for the latest JRuby release.
2. Unpack JRuby: Unpack the file you downloaded. You’ll then have a "jruby-" directory.
3. Run JRuby: The JRuby startup script lives in the "bin" directory. Run "bin/jruby -v" to confirm it.

I found that the following steps are also required:

[greg:jruby-1.5.0.RC1] export JRUBY_HOME=/Users/greghelton/dev/bin/jruby-1.5.0.RC1
[greg:jruby-1.5.0.RC1] export PATH=$PATH:$JRUBY_HOME/bin

Then, to run jirb:

[greg:jruby-1.5.0.RC1] bin/jirb

Or, to run sinatra, first edit hi.rb

require 'rubygems'
require 'sinatra'
get '/hi' do
  "Hello World!"
end

and run the class

[greg:project1] sudo jgem install sinatra
[greg:project1] jruby ./hi.rb
== Sinatra/0.9.4 has taken the stage on 4567 for development with backup from WEBrick
[2010-04-16 18:09:50] INFO  WEBrick 1.3.1
[2010-04-16 18:09:50] INFO  ruby 1.8.7 (2010-04-14) [java]
[2010-04-16 18:09:50] INFO  WEBrick::HTTPServer#start: pid=4475 port=4567

then open your browser to ...

http://localhost:4567/hi

Installing activerecord for JRuby

This site is a big help but I had to do things a little differently. The command 'rake jgem' didn't work but 'rake gem' did then, strangely, 'jgem install ...' worked as the next step.
[greg:ruby] export JRUBY_HOME=/Users/greghelton/dev/bin/jruby-1.5.0.RC1
[greg:ruby] export PATH=$PATH:$JRUBY_HOME/bin
[greg:ruby] mkdir active
[greg:ruby] cd active
[greg:active] git clone git://github.com/rails/rails.git
Initialized empty Git repository in /Users/greghelton/dev/src/ruby/active/rails/.git/
remote: Counting objects: 163266, done.
remote: Compressing objects: 100% (36468/36468), done.
remote: Total 163266 (delta 125957), reused 162169 (delta 125035)
Receiving objects: 100% (163266/163266), 28.37 MiB | 326 KiB/s, done.
Resolving deltas: 100% (125957/125957), done.
[greg:active] cd rails
[greg:rails] rake gem
             ... many lines reported by the build ...
[greg:rails] ls -al activesupport/pkg
total 584
drwxr-xr-x   3 greghelton  staff     102 Apr 22 01:19 .
drwxr-xr-x  12 greghelton  staff     408 Apr 22 01:19 ..
-rw-r--r--   1 greghelton  staff  295424 Apr 22 01:19 activesupport-3.0.0.beta3.gem
[greg:rails] sudo jgem install ./activesupport/pkg/activesupport-3.0.0.beta3.gem
Password:
Successfully installed i18n-0.3.7
Successfully installed tzinfo-0.3.20
Successfully installed memcache-client-1.8.2
Successfully installed activesupport-3.0.0.beta3
4 gems installed
Installing ri documentation for i18n-0.3.7...
Installing ri documentation for tzinfo-0.3.20...
Installing ri documentation for memcache-client-1.8.2...
Installing ri documentation for activesupport-3.0.0.beta3...
Installing RDoc documentation for i18n-0.3.7...
Installing RDoc documentation for tzinfo-0.3.20...
Installing RDoc documentation for memcache-client-1.8.2...
Installing RDoc documentation for activesupport-3.0.0.beta3...
[greg:rails] ls -al activemodel/pkg
total 72
drwxr-xr-x   3 greghelton  staff    102 Apr 22 01:19 .
drwxr-xr-x  11 greghelton  staff    374 Apr 22 01:19 ..
-rw-r--r--   1 greghelton  staff  33792 Apr 22 01:19 activemodel-3.0.0.beta3.gem
[greg:rails] sudo jgem install ./activemodel/pkg/activemodel-3.0.0.beta3.gem
Successfully installed activemodel-3.0.0.beta3
1 gem installed
Installing ri documentation for activemodel-3.0.0.beta3...
Installing RDoc documentation for activemodel-3.0.0.beta3...
[greg:rails] jirb
irb(main):013:0> require 'active_model'
=> true
irb(main):014:0> class Person                                       
irb(main):015:1>   include ActiveModel::Validations                 
irb(main):016:1>   validates_presence_of :first_name, :last_name    
irb(main):017:1>   attr_accessor :first_name, :last_name            
irb(main):018:1>   def initialize(first_name, last_name)            
irb(main):019:2>     @first_name, @last_name = first_name, last_name
irb(main):020:2>   end                                              
irb(main):021:1> end                                                
=> nil
irb(main):022:0> a = Person.new("Barney", "Rubble")
=> #
irb(main):023:0> a.valid?
=> true
irb(main):024:0>
Super!