1. Ruby Ruby on Rails Demo Summary JRuby
Ruby on Rails
Tristan Bigourdan
David Alphen
Sales & e-Commerce Platform
Server Architecture & Frameworks
Internship 2007
2. Ruby Ruby on Rails Demo Summary JRuby
Focus
1 Ruby
History
In Brief
Language Tour
2 Ruby on Rails
Introduction
Rails MVC
Components
3 Demo
4 Summary
Strengths and Weaknesses
Key benefits
5 JRuby
JRuby
JRuby on Rails
Future of JRuby
3. Ruby Ruby on Rails Demo Summary JRuby
Focus
1 Ruby
History
In Brief
Language Tour
2 Ruby on Rails
Introduction
Rails MVC
Components
3 Demo
4 Summary
Strengths and Weaknesses
Key benefits
5 JRuby
JRuby
JRuby on Rails
Future of JRuby
4. Ruby Ruby on Rails Demo Summary JRuby
Focus
1 Ruby
History
In Brief
Language Tour
2 Ruby on Rails
Introduction
Rails MVC
Components
3 Demo
4 Summary
Strengths and Weaknesses
Key benefits
5 JRuby
JRuby
JRuby on Rails
Future of JRuby
5. Ruby Ruby on Rails Demo Summary JRuby
Focus
1 Ruby
History
In Brief
Language Tour
2 Ruby on Rails
Introduction
Rails MVC
Components
3 Demo
4 Summary
Strengths and Weaknesses
Key benefits
5 JRuby
JRuby
JRuby on Rails
Future of JRuby
6. Ruby Ruby on Rails Demo Summary JRuby
Focus
1 Ruby
History
In Brief
Language Tour
2 Ruby on Rails
Introduction
Rails MVC
Components
3 Demo
4 Summary
Strengths and Weaknesses
Key benefits
5 JRuby
JRuby
JRuby on Rails
Future of JRuby
7. Ruby Ruby on Rails Demo Summary JRuby
History
History
Developed by Yukihiri Matsumoto a.k.a "Matz"
Inspired by Smalltalk, Python and Perl
1993 : Creation
1995 : First release
2000 : USA adoption (books, documentation)
March 2007 : release 1.8.6
Famous in Japan and USA (especially through rails)
Collaborative platform : http ://rubyforge.org
8. Ruby Ruby on Rails Demo Summary JRuby
In Brief
Main features
Ruby is a dynamic, object oriented and interpreted
language written in C and open source.
Focus on simplicity, productivity and "fun"
9. Ruby Ruby on Rails Demo Summary JRuby
Language Tour
Everything is Object
Pure Object Oriented language
Absolutely everything is an object
nil.class => NilClass
//.class => Regexp
"Amadeus".length => 7
3.times { |i| puts "Number #{i}" }
=> Number 1
=> Number 2
=> Number 3
10. Ruby Ruby on Rails Demo Summary JRuby
Language Tour
Dynamic Typing
Dynamically typed language
No need to declare variables
Duck Typing
"If the object quacks and walks like a duck then it’s a duck"
The type of an object is defined by what that object can do
Example
class NerdConnexion
def talk
"I’m a nerd 2.0"
end
end
nerd = NerdConnexion.new
nerd.respond_to ?( :talk) => true
11. Ruby Ruby on Rails Demo Summary JRuby
Language Tour
Dynamic Typing
Dynamically typed language
No need to declare variables
Duck Typing
"If the object quacks and walks like a duck then it’s a duck"
The type of an object is defined by what that object can do
Example
class NerdConnexion
def talk
"I’m a nerd 2.0"
end
end
nerd = NerdConnexion.new
nerd.respond_to ?( :talk) => true
12. Ruby Ruby on Rails Demo Summary JRuby
Language Tour
Dynamic Typing
Dynamically typed language
No need to declare variables
Duck Typing
"If the object quacks and walks like a duck then it’s a duck"
The type of an object is defined by what that object can do
Example
class NerdConnexion
def talk
"I’m a nerd 2.0"
end
end
nerd = NerdConnexion.new
nerd.respond_to ?( :talk) => true
13. Ruby Ruby on Rails Demo Summary JRuby
Language Tour
Metaprogramming
Create code dynamically
Modify existing classes
add/alias/remove/replace methods
include a Module, Mixin
add instance/class variable
method_missing() const_missing()
Dynamic finders
find_all_by_name_and_age(’john’,42)
find_or_create
Query objects about their methods
obj.methods
14. Ruby Ruby on Rails Demo Summary JRuby
Language Tour
Design Pattern 1/3
Core Implementation
Singleton
Factory
Observer
15. Ruby Ruby on Rails Demo Summary JRuby
Language Tour
Design Pattern 2/3
Personal Implementation
class MySingleton
private_class_method :new
@@class_variable = nil
def MySingleton.create
@@class_variable = new unless @@class_variable
@@class_variable
end
end
Problem
Not thread safe
16. Ruby Ruby on Rails Demo Summary JRuby
Language Tour
Design Pattern 2/3
Personal Implementation
class MySingleton
private_class_method :new
@@class_variable = nil
def MySingleton.create
@@class_variable = new unless @@class_variable
@@class_variable
end
end
Problem
Not thread safe
17. Ruby Ruby on Rails Demo Summary JRuby
Language Tour
Design Pattern 3/3
Core Implementation
require singleton
class CoreSingleton
attr_accessor :new
include Singleton
end
Result
Thread safe
18. Ruby Ruby on Rails Demo Summary JRuby
Language Tour
Design Pattern 3/3
Core Implementation
require singleton
class CoreSingleton
attr_accessor :new
include Singleton
end
Result
Thread safe
19. Ruby Ruby on Rails Demo Summary JRuby
Language Tour
Single Inheritance
Code example Mixins
module Shape
attr_accessor :type
A module included in a class
attr_accessor :color
def initialize(type, color)
@type = type
@color = color
end
end
class Square
include Shape
attr_accessor :side
def perimeter
side * 4
end
end
s = Square.new "Square", "Red"
s.side = 6
class « s
def area
side * side
end
end
20. Ruby Ruby on Rails Demo Summary JRuby
Language Tour
Containers
Block, Iterator, Array
{} or do . . .end
Syntactical sugar
Example
tab = [1,2,3,4,5,6,7,8,9]
tab.each do
|i| print i
end
%w(un dos tres).each {|p| print p}
[1,2,3,4,5,6,7,8,9].each_index {|i| print i}
21. Ruby Ruby on Rails Demo Summary JRuby
Language Tour
Containers
Block, Iterator, Hash
my_hash = {
:key1 => "value1",
:key2 => "value2"
}
def aff
yield.each_key {|val| print val}
end
aff {my_hash}
22. Ruby Ruby on Rails Demo Summary JRuby
Introduction
What is Ruby on Rails
Creator : David Heinemeier Hansson
First stable version : 12.2005
March 07 : rails 1.2.3
MIT Licence
Rails is a full stack web-application framework following the
MVC pattern which includes :
an ORM package ActiveRecord,
a template engine,
a controller framework,
everything needed to develop web-apps that can run on
CGI, FastCGI, and mod_ruby.
23. Ruby Ruby on Rails Demo Summary JRuby
Introduction
Principles
1 Convention over configuration
avoid XML configuration
can be overridden
2 DRY : Don’t Repeat Yourself
Reduce code redundancy
3 Agile development environment
No recompile, deploy, restart cycles
Simple tools to generate code quickly
Testing built into framework
24. Ruby Ruby on Rails Demo Summary JRuby
Introduction
Why Ruby on Rails ?
Created while solving a problem from the real world
Agile Development
Develop fast web applications
Simplicity
Web 2.0
25. Ruby Ruby on Rails Demo Summary JRuby
Introduction
Why Ruby ?
Ruby’s Metaprogramming
Ruby’s Librairies
Ruby’s simplicity and efficiency
Rails is written in Ruby
Share parent child relationships
Ruby is the father, Rails is the child
Same conventions
Develop faster
Better productivity
26. Ruby Ruby on Rails Demo Summary JRuby
Rails MVC
The Rails MVC
27. Ruby Ruby on Rails Demo Summary JRuby
Components
Components Reminder
Active Record Rails is a Full Stack Web Framework.
Action Pack
Scaffolding
Database Versioning
Ajax Integration
Tests
Active Support
Action Mailer
Action Web Services
Plugins
Scripts
...
28. Ruby Ruby on Rails Demo Summary JRuby
Components
Components Reminder
Active Record Rails is a Full Stack Web Framework.
Action Pack
Scaffolding
Database Versioning
Ajax Integration
Tests
Active Support
Action Mailer
Action Web Services
Plugins
Scripts
...
29. Ruby Ruby on Rails Demo Summary JRuby
Components
Active Record 1/3
Based on a design pattern defined by Martin Fowler
ORM layer supplied with Rails
No more SQL
Tables map to classes
Rows map to objects
Columns map to attributes
Relies on convention
Validation
Transaction
30. Ruby Ruby on Rails Demo Summary JRuby
Components
Active Record 2/3
Conventions
1 Table names
Plural form of the Class
Class Flight => flights
Class MyFlight => my_flights
2 Turn this off
in config/environment.rb :
ActiveRecord : :Base.pluralize_table_names = false
in the model :
class Flight < ActiveRecord : :Base
set_table_name "db_flights"
end
31. Ruby Ruby on Rails Demo Summary JRuby
Components
Active Record 3/3
Conventions
1 Primary keys
Default name : id
Can be manually defined
class Flight < ActiveRecord : :Base
set_primary_key "flight_id"
end
2 Foreign keys
By default [singular_name_id]
flight_id
No foreign key constraints => Use SQL
32. Ruby Ruby on Rails Demo Summary JRuby
Components
Action Pack
Action Controller
Routing
Cookies and Sessions
Filters
Caching
Action View
Helpers
rhtml
rjs
rxml
layouts, partials
33. Ruby Ruby on Rails Demo Summary JRuby
Components
Action Pack
Action Controller
Routing
Cookies and Sessions
Filters
Caching
Action View
Helpers
rhtml
rjs
rxml
layouts, partials
34. Ruby Ruby on Rails Demo Summary JRuby
Components
Scaffolding
Manipulate the model
CRUD
Administration
35. Ruby Ruby on Rails Demo Summary JRuby
Components
Database Versioning
Starting point of the application
Evolves with the application
Data and Schema migrations
Database Independant
Constraints and Stored procedures not supported
=> Native SQL
Caution : rollback
36. Ruby Ruby on Rails Demo Summary JRuby
Components
Web 2.0
Ajax
Prototype
Simplifies the use of Javascript
Script.aculo.us
Built on top of Prototype
Autocompletion, Drag and Drop,
Visual effects, Observers, . . .
38. Ruby Ruby on Rails Demo Summary JRuby
Strengths and Weaknesses
Strengths Weaknesses
Conventions Maturity (Specifications,
Feedback loop Documentation)
Active Community Independant groups of
developers
Full stack framework
Connection pooling, JMS
Ruby’s strengths
Performance
Ajax
Deployment on the
Windows platform
New way of developping,
thinking
IDE
39. Ruby Ruby on Rails Demo Summary JRuby
Strengths and Weaknesses
Strengths Weaknesses
Conventions Maturity (Specifications,
Feedback loop Documentation)
Active Community Independant groups of
developers
Full stack framework
Connection pooling, JMS
Ruby’s strengths
Performance
Ajax
Deployment on the
Windows platform
New way of developping,
thinking
IDE
40. Ruby Ruby on Rails Demo Summary JRuby
Key benefits
Conclusion
Fast development
Better productivity
Reduce costs
Write code, not configuration
41. Ruby Ruby on Rails Demo Summary JRuby
JRuby
JRuby
100% pure java implementation of the Ruby interpreter
Created in 2002
Open source, many active contributors
Two main contributors recently hired by Sun Microsystems
to work full time
Aims at compatibility with current Ruby version
Current version : 1.0
42. Ruby Ruby on Rails Demo Summary JRuby
JRuby
Java from JRuby 1/2
Using Java from JRuby
Import and use java packages and classes within JRuby
scripts
Subclass java classes with JRuby classes
Implement java interfaces with JRuby classes
43. Ruby Ruby on Rails Demo Summary JRuby
JRuby
Java from JRuby 2/2
Power of java technology with Ruby syntax
set = java.util.TreeSet.new
set.add "foo"
set.add "Bar"
set.add "Baz"
set.each do |v| puts "value : #{v}" end
list = java.util.ArrayList.new
list « 1
list « 3
list « 2
list.sort
44. Ruby Ruby on Rails Demo Summary JRuby
JRuby
JRuby from Java 1/2
Call from java applications to JRuby scripts through JSR 223
JSR-223 : simple and small API that specifies the contract
for communicating with a scripting language from java and
vise versa
API introduced in java SE 6 in package javax.script
Allows to :
Call scripts, script functions, methods and objects from Java
Access java objects and classes from scripts
45. Ruby Ruby on Rails Demo Summary JRuby
JRuby
JRuby from Java 2/2
Integrate Java with JRuby using JSR-223
//Create a JRuby engine
ScriptEngine jrEngine = factory.getEngineByName("jruby") ;
// Make available a java object to JRuby
jrEngine.put("javaobject", "dummy_string") ;
// Print dummy_string from JRuby code
try {
jrEngine.eval("puts(javaobject)") ;
}catch (ScriptException exception) {
exception.printStackTrace() ;
}
46. Ruby Ruby on Rails Demo Summary JRuby
JRuby
Why JRuby ?
JRuby over Ruby
Compilation
Better performance and scalability than Ruby
(eg : native threading)
Native unicode support
Java API + java libraries
JRuby over Java
Ruby language : dynamic typing, blocks, modules,
metoprogramming
Ruby applications : Ruby on Rails, Rake, Raven etc
47. Ruby Ruby on Rails Demo Summary JRuby
JRuby
Why JRuby ?
JRuby over Ruby
Compilation
Better performance and scalability than Ruby
(eg : native threading)
Native unicode support
Java API + java libraries
JRuby over Java
Ruby language : dynamic typing, blocks, modules,
metoprogramming
Ruby applications : Ruby on Rails, Rake, Raven etc
48. Ruby Ruby on Rails Demo Summary JRuby
JRuby on Rails
Jruby on Rails
Part of JRuby project
Brings power and functionality of Java to Ruby on Rails :
Call scripts, script functions, methods and objects from Java
Deployment of ROR apps to JEE application servers
(switch framework VS switch whole architecture)
Java scalability (eg : database pooling)
Java libraries
Integration with java legacy apps
49. Ruby Ruby on Rails Demo Summary JRuby
JRuby on Rails
Jruby on Rails
Part of JRuby project
Brings power and functionality of Java to Ruby on Rails :
Call scripts, script functions, methods and objects from Java
Deployment of ROR apps to JEE application servers
(switch framework VS switch whole architecture)
Java scalability (eg : database pooling)
Java libraries
Integration with java legacy apps
50. Ruby Ruby on Rails Demo Summary JRuby
Future of JRuby
JRuby Future ?
RoR lack of maturity
JRuby : bring power of JVMs, JEE application servers and
libraries to RoR
=> lower barrier of entry
Glassfish V3 (Sun JEE application server) will allows to run
other containers than JEE : RoR, PHP etc
51. Ruby Ruby on Rails Demo Summary JRuby
Future of JRuby
Q&A
Thank you
52. Ruby Ruby on Rails Demo Summary JRuby
Future of JRuby
Ruby on Rails
Tristan Bigourdan
David Alphen
Sales & e-Commerce Platform
Server Architecture & Frameworks
Internship 2007
53. Appendix
Bibliographie
More Information I
Dave Thomas
Programming Ruby 2nd edition.
Pragmatic Programmers 2005.
David Heinemeier Hansson
Agile Web Development with Rails 2nd edition.
Pragmatic Programmers 2007.
Memo
http ://www.zenspider.com/Languages/Ruby/QuickRef.html
Good memo.
Ruby Doc
http ://www.ruby-doc.org
Standard and Core Librairies APIs.
54. Appendix
Bibliographie
More Information II
Ruby Garden
http ://www.rubygarden.org
Ruby tutorials, wiki, . . .
Ruby On Rails
http ://www.rubyonrails.org
RoR doc, tutorials, wiki, . . .
JRuby
http ://www.jruby.org
JRuby doc, tutorials, wiki, . . .