Thursday May 17, 2007
Getting Started with JRuby - Tutorial at RailsConf rails
![]() |
Earlier today, I attended Charles Nutter and Thomas Enebo "Your First Day with JRuby on Rails" tutorial at Rails Conf 2007. The key message is Ruby as the programming language and Java for the platform and libraries provides the best of both worlds to developers. |
|
|
|
|
JRuby is 100% pure-Java implementation of the
Ruby programming language. The goal is to use the power of Ruby programming
language and leverage the power of Java platform. Read a more detailed tutorial
here. All Ruby scripts work with JRuby instead of the original C-based
implementation. Also the goal is NOT an attempt to alter Ruby or add
incompatible features. You can download
JRuby implementation, unzip it and
set JRUBY_HOME to the installation directory. Here is a sample JRuby code:
1. require 'java'
2. include_class 'java.lang.ArrayList'
3. include_class 'javax.swing.JFrame'
4.
5. list = ArrayList.new
6. frame = JFrame.new("Ruby SWINGS!")
7. list << frame
8. list.each { |f| f.set_size(200, 200) }
9. puts frame.title
The code above shows how Ruby and Java code can be mixed with each other.
Line 1 includes Java support. Line 2 imports Java classes. Line 5 and 6 creates
a new variables using a mix of Java and Ruby syntax. Line 7 and 8 uses the
instance variables in Ruby syntax. Line 9 prints the title of the frame. If you
run this code using JRuby interpreter then you get:
%JRUBY_HOME%/bin/jruby sample1.rb
Ruby SWINGS!
Here are some of the differences (from the original C-based implementation) in JRuby:
database.yml' fromdevelopment:
adapter: mysql
database: HelloWorld_development
username: root
password:
host: localhost
to
development:
adapter: jdbc
driver: com.mysql.jdbc.Driver
url: jdbc:mysql://localhost:3306/helloworld_development
username: root
password:require 'jdbc_adapter'" in 'environment.rb'.work/jruby/lib'
directory.Here are some other points that the tutorial talked/showed:
gem install glassfish-rails
jruby script/server glassfish
=> Starting GlassFish
=> Rails application on http://localhost:8080/8080
=> Rails application on http://localhost:8080/4848
=> Clustering enabled
=> Connecting pooling enabled
=> Load balancing enabled
=> Server Ready.The biggest pain point from the audience was Rails deployment. With JRuby and GlassFish, you can continue deploying your apps on Mongrel. Because of JRuby, it also allows you to deploy your RoR applications on GlassFish, open-source, production-quality, Java EE 5 compatible application server.
BTW, there is supposed to be wireless connectivity through out the conference but it's very spotty :(
Technorati: glassfish rubyonrails ror netbeans railsconf
Posted by Arun Gupta in web2.0 | Comments[4]
|
|
|
|
| Craig announced jMaki on Rails. Here I provide detailed steps to read data from a MySQL database and display it in a jMaki-wrapped Yahoo DataTable widget.
Tools', 'Plugins',
select 'Downloaded', click on 'Add
Plugins ...'.Tools', 'Plugins',
select 'Installed'. It should show 'jMaki
Ajax support' with '1.6.9.7'
version number.jmaki_ror'.Generate ...', select 'controller',
specify the Name as 'jmaki' and Views as 'yahoo'.script/plugin source
http://jmaki-goodies.googlecode.com/svn/trunk/rails_plugins
script/plugin install jmaki_core
script/plugin install jmaki_yahoo
The first step adds the plug-in registry to the list of
registries used for searching plug-ins. The second two steps install the
core and yahoo plug-ins.Views', right-click on
'layouts', select 'New',
select 'Empty RHTML (Embedded Ruby) file ...'.
Enter the 'File Name' as 'standard'.
This will add 'standard.rhtml' in layouts
sub-tree. Enter the following fragment before <body>:<head>
<%= stylesheet_link_tag "jmaki-standard", :media
=> "all" -%>
<%= javascript_include_tag "jmaki" -%>
<%= jmaki_setup -%>
</head><%= @content_for_layout %>Controllers', 'jmaki_controller.rb',
add the following fragment before 'def yahoo'
line:layout "standard"mysqladmin -u root create
jmaki_ror_development'. You need to make sure that the database
name in this script is changed to match the project name.Generate',
select 'model', specify the arguments as 'grid',
click 'OK'. This will
generate, in NetBeans project, Database Migrations, migrate,
001_create_grids.rb.001_create_grids.rb,
change self.up
helper
method such that it looks like:def self.up
create_table :grids do |t|
t.column :company, :string
t.column :price, :float
t.column :change, :float
t.column :percent_change, :float
t.column :last_updated, :string
end
end Run
Rake
Target', 'db', 'migrate'.
This generates the database
table. Execute the
following query to insert data into the table:insert into grids values (1, 'A Co', 71.72,
0.02, 0.03, 'Jan 1, 2007, 10:00am' );
insert into grids values (2, 'B Inc', 29.01, 0.42, 1.47, 'Feb 1, 2007,
10:00am' );
insert into grids values (3, 'C Group Inc', 83.81, 0.28, 0.34, 'Mar 1,
2007, 10:00am' );
insert into grids values (4, 'D Company', 52.55, 0.01, 0.02, 'Apr 1,
2007, 10:00am' );Views',
'jmaki', 'yahoo.rhtml',
drag-and-drop 'Data Table' widget from the 'Yahoo'
category of jMaki palette.
yahoo.rhtml' with the
following:<%= jmaki_widget 'yahoo.dataTable',
:args => {
:columns => [
{ :title => 'Company', :width => 200, :locked =>
false },
{ :title => 'Price', :width => 75, :renderer =>
'usMoney' },
{ :title => 'Change', :width => 75, :renderer =>
'change' },
{ :title => '% Change', :width => 75, :renderer =>
'pctChange' },
{ :title => 'Last Updated', :width => 85, :renderer
=> 'italic' }
]
},
:value => @table_data
-%>@table_data' is defined
in 'jmaki_controller' in 'def yahoo'
as:def yahoo
@table_data = []
Grid.find_all().each do |data|
@table_data
<< [ data.company, data.price, data.change,
data.percent_change, data.last_updated]
end
endhttp://localhost:3000/jmaki/yahoo'.
Now jMaki-wrapped Yahoo DataTable widget is picking data from the MySQL
database. And as a next step, you can WAR'up this RoR application and deploy it on GlassFish V2 using these instructions.
Technorati: jmaki rubyonrails ror netbeans mysql glassfish
Posted by Arun Gupta in web2.0 | Comments[4]
|
|
|
|
| Database-enabled RoR WAR on GlassFish
UPDATE: Simplified steps for GlassFish V2 are available here and for V3 here.
Follow up from here.In this post I'll show how a Ruby-on-Rails (RoR) application, talking to MySQL database, can be deployed as a WAR file on GlassFish V2. Here are the steps I followed:
Configuration' and open 'database.yml'.
Change production database entry from:production:
adapter: mysql
database: HelloWorld_production
username: root
password:
host: localhost
production:
adapter: jdbc
driver: com.mysql.jdbc.Driver
url: jdbc:mysql://localhost:3306/helloworld_development
username: root
password:Configuration', 'environment.rb', addrequire 'jdbc_adapter'Rails::Initializer.run do |config|if RUBY_PLATFORM =~ /java/
require 'rubygems'
RAILS_CONNECTION_ADAPTERS = %w(jdbc)
end
rake war:standalone:create' from your application directory.
domains/domain1/autodeploy"
directory of your GlassFish instance and you are done!With this, you've deployed a Ruby-on-Rails application accessing MySQL database as a WAR file on GlassFish V2.
Technorati: glassfish rubyonrails ror netbeans jruby ruby
Posted by Arun Gupta in web2.0 | Comments[7]
|
|
|
|
|
Today's Page Hits: 2305
Total # blog entries: 1009