java, ruby & rails
DESCRIPTION
An introduciton to the ruby/jruby ecosystem. Also touching a bit on Rails. Presented internally for our Java consultantsTRANSCRIPT
C O N N E C T I N G B U S I N E S S & T E C H N O L O G Y
Java, Ruby & RailsJava, Ruby & Rails
2009-03-18
2 C O N N E C T I N G B U S I N E S S & T E C H N O L O G Y
The Java platform
“It’s all about the Java Virtual Machine. That’s the integration hub.” Gosling, Sun Tech Days 2007Lots of new languages for the JVM Scala, Clojure, Groovy, Jython, (J)Ruby, JavaScript
(Rhino), JavaFXThe DaVinci Machine JSR-292 Target JDK 7 invokedynamic instruction
2009-03-18
3 C O N N E C T I N G B U S I N E S S & T E C H N O L O G Y
Why Ruby?
Ubiquity C-Ruby (MRI) JRuby IronRuby (.NET) Rubinius, Maglev, YARV
Scripting java shebang/backticks
Rails FrameworkSupport from Sun, ThoughtworksMost likely to succeed?
2009-03-18
4 C O N N E C T I N G B U S I N E S S & T E C H N O L O G Y
What is Ruby?
Created by Yukihiro Matsumoto (Matz) in 1993“A dynamic, open source programming language with a focus on simplicity and productivity. It has an elegant syntax that is natural to read and easy to write.” The principle of least surpriseRuntime efficiency not a top priority“More powerful than Perl, more OO than Python”MRI is the specification RubySpec is in the works
2009-03-18
5 C O N N E C T I N G B U S I N E S S & T E C H N O L O G Y
Ruby Characteristics
InterpretedObject oriented ”myString”.upcase 2_500.+2 44.modulo 5
ReflectiveGarbage collectedDuck typing If it walks like a duck and quacks like a duck it must be a duck
2009-03-18
6 C O N N E C T I N G B U S I N E S S & T E C H N O L O G Y
Dynamic features
Reopen existing classes redefine methods “monkeypatching”
Meta-programmingMethod aliasing alias_method :validates_size_of, :validates_length_of
method_missing Eg provide a generic sort method sort_by_x
Closures
2009-03-18
7 C O N N E C T I N G B U S I N E S S & T E C H N O L O G Y
Core Ruby tools
(j)ruby the interpreter
(j)irb interactive ruby, ~readline support, tab completion
ri ruby interactive ~man pages
rdoc html doc ~javadoc
rake ant/make for ruby
gem package manager
2009-03-18
8 C O N N E C T I N G B U S I N E S S & T E C H N O L O G Y
Ruby anatomy
Classes are CamelCasedMethods are under_scoredGenerally no need for curly braces, parentheses, semicolon or return statement code more compact
Comments =begin block comment =end # line comment
Variables Constant @@classVariable @instanceVariable localVariable
:symbol
2009-03-18
9 C O N N E C T I N G B U S I N E S S & T E C H N O L O G Y
Example
2009-03-18
10 C O N N E C T I N G B U S I N E S S & T E C H N O L O G Y
Ruby structure
Modules Module::Class Mix-ins interfaces with functionality Namespace
Subclass < Superclass Single inheritance only
Files are named *.rb can hold many classes
require ’file’include Modulesearches $LOAD_PATH
2009-03-18
11 C O N N E C T I N G B U S I N E S S & T E C H N O L O G Y
Access modifiers
private Private to this instance (might be a subclass) You can’t specify the receiver, not even self
protected Can be called by instances of the same class Eg use for comparators
public FFA
private :method or private (until end)The modifiers are methods, not keywords
2009-03-18
12 C O N N E C T I N G B U S I N E S S & T E C H N O L O G Y
Testing
Test::Unit::TestCase xUnit for ruby
JRuby to test Java code? JtestR to invoke Code coverage
might be an issueRSpec BDD
2009-03-18
13 C O N N E C T I N G B U S I N E S S & T E C H N O L O G Y
Method signature - Java
Method overloadingNumber of method signatures increases fast if flexibility is wanted
2009-03-18
14 C O N N E C T I N G B U S I N E S S & T E C H N O L O G Y
Method signature – PL/SQL
Named parametersStill have to change method signature too add parameters
2009-03-18
15 C O N N E C T I N G B U S I N E S S & T E C H N O L O G Y
Method signature – Ruby
Hash as optional parameter, not named parametersNo need to change method signatureHash doesn’t need curly bracesMethod overload unavailable*args => arguments array
2009-03-18
16 C O N N E C T I N G B U S I N E S S & T E C H N O L O G Y
More syntax examples
=~ regexp matcher myString =~ /[0-9]+/%w[] String array myArray = %w[ruby java haskell]{||} or do || end closures (and embedded variables) myHash.each { |k, v| puts k+’ is ‘+v } myHash.each do |key, val|
puts “#{key} is #{val}”end
2009-03-18
17 C O N N E C T I N G B U S I N E S S & T E C H N O L O G Y
Ruby on Rails
MVC Framework“Rails is the most well thought-out web development framework I’ve ever used. And that’s in a decade of doing web applications for a living. I’ve built my own frameworks, helped develop the Servlet API, and have created more than a few web servers from scratch. Nobody has done it like this before.” -James Duncan Davidson, Creator of Tomcat and Ant“Rails is the killer app for Ruby.” Yukihiro Matsumoto, Creator of Ruby
2009-03-18
18 C O N N E C T I N G B U S I N E S S & T E C H N O L O G Y
RoR principles
Opinionated softwareConvention over configurationDRYREST GET /products #get all GET /products/42 #get id=42 POST /products #create
Fast feedback loop just reload, no compile
Rails itself is a gem
2009-03-18
19 C O N N E C T I N G B U S I N E S S & T E C H N O L O G Y
Ruby on Rails concepts
rails <appname> generates skeleton routes.rb ~struts-config.xml*.html.erb ~JSPGenerators Generates model, view, controller, tests
Partials (html.erb snippets)Plugins (instead of gems)Default database is sqlite3 (via JDBC for JRuby)Filters (defined in controller)
2009-03-18
20 C O N N E C T I N G B U S I N E S S & T E C H N O L O G Y
RoR tools
rails <appname> -d <database>script/generate scaffold Person name:stringscript/serverscript/console irb with access to the application
script/dbconsole enter SQL
script/server –debugger debugger in the code drops to console
2009-03-18
21 C O N N E C T I N G B U S I N E S S & T E C H N O L O G Y
ActiveRecord O/R mapping
Db migrations, dev, test, prod up and down database agnostic rake db:migrate config/database.yml
Opinionated software primary key named id pluralization class Book => db books
2009-03-18
22 C O N N E C T I N G B U S I N E S S & T E C H N O L O G Y
RoR model example
class BlogPost < ActiveRecord::Base belongs_to :author has_many :comments validates_presence_of :titleend
comments table needs blog_post_idblog_posts table needs author_idNew post need title field
2009-03-18
23 C O N N E C T I N G B U S I N E S S & T E C H N O L O G Y
A/R Metaprogramming
2009-03-18
24 C O N N E C T I N G B U S I N E S S & T E C H N O L O G Y
A/R Metaprogramming
2009-03-18
25 C O N N E C T I N G B U S I N E S S & T E C H N O L O G Y
ActiveSupport::TestCase
Inherits Test::Unit::TestCase Provides helpers, eg http calls
<app>/test functional integration unit
fixtures/fixtures.ymlrake
2009-03-18
26 C O N N E C T I N G B U S I N E S S & T E C H N O L O G Y
Jruby Motivation
The JVMSneak into the enterpriseReuse infrastructureIntegrate with Java applicationsPerformance and scalability
2009-03-18
27 C O N N E C T I N G B U S I N E S S & T E C H N O L O G Y
JRuby on Rails
Develop as Java Netbeans Eclipse
Integrate with Java Call EJBs Use JMS queues Use JNDI Use JAAS
Deploy on Java As *.war using warbler Rails deployer (JBoss, Glassfish)
2009-03-18
28 C O N N E C T I N G B U S I N E S S & T E C H N O L O G Y
Java integration
include Java / require ”java”include Java::JavaxSwinginclude_class "javax.naming.InitialContext”require ”path/to/my.jar”prefix javaclasses to avoid name collisions String => JString include_class 'java.lang.String’ {|package,name| "J#{name}" }
Method alias System.currentTimeMillis => System.current_time_millis
Getters and setters behaves like attr_accessor fields
2009-03-18
29 C O N N E C T I N G B U S I N E S S & T E C H N O L O G Y
IDE support
Eclipse - DLTK (Dynamic Languages Toolkit) Eclipse foundation plugin Support for TCL, Python, Ruby No Rails
Eclipse - Aptana RadRails RDT Ruby Editor Rails support generators/scripts Visual debug Testrunner
Netbeans Official plugin
2009-03-18
30 C O N N E C T I N G B U S I N E S S & T E C H N O L O G Y
Conclusion
Ruby is sweet! Syntactic sugar Fun to work with
Ruby is useful! Integrate with Java OO scripting Rails is probably fast enough
Ruby is hard! “Good programmers become better, bad programmers
become worse” Test, test, test
2009-03-18
31 C O N N E C T I N G B U S I N E S S & T E C H N O L O G Y
Thank you and Namaste
2009-03-18
32 C O N N E C T I N G B U S I N E S S & T E C H N O L O G Y
Contact
© Devoteam Consulting A/S.This document is not to be copied or
reproduced in any way without the express permission of Devoteam Consulting.
AUSTRIA
BELGIUM
CZECH REPUBLIC
DENMARK
FRANCE
MOROCCO
NETHERLANDS
NORWAY
POLAND
SAUDI ARABIA
SPAIN
SWEDEN
SWITZERLAND
UNITED ARAB EMIRATES
UNITED KINGDOM
CONTACT
Person: Peter Sönnergren
Phone: +46 (0)733-812135
E-mail: [email protected]
Address
DOCUMENT
ID: #
Author:
Date: