Ruby on Rails Une introduction
beaucoup de bruit !
Les gurus
* James Duncan Davidson (ANT et Tomcat) * Mike Clark (Pragmatic Automation) * Jason Hunter (Java Servlet Programming) * Bruce Tate (Bitter Java, Spring Dev Notebook) * Dion Almaer (Founder of theserverside.com) * Stuart Holloway (Component Dev for Java) * Justin Gehtland (Better, Faster, Lighter Java) * Glenn Vanderburg (Tricks of the Java Programming Gurus) * David Geary (Graphic Java, Core JSF) ??
Martin Fowler
(25 September 2005) In the last year we've seen a explosion of interest in Ruby, particularly driven by the web application framework Ruby on Rails. I haven't had the opportunity to play with it, but I've heard strong endorsements of it from many people whose judgment I trust, both inside and outside ThoughtWorks.
(10 May 2006) So far the results are firmly in favor of Ruby. When I ask the question "do you think you're significantly more productive in Ruby rather than Java/C#", each time I've got a strong 'yes'.
JSF Expert Group Member
David Geary
February 2005
With it's killer support for database integration and default view generation, perhaps even 10X as fast as a comparable Java framework. But it seems to me that once both apps are up and running, ROR is no match for a Javabased web app six pack of JSF-Shale-Tiles-SiteMeshHibernate-Spring
Déjà imité !
Django in Python Fanery in Python Subway also in Pyton TurboGears another Python framework Catalyst in Perl TrimJunction in JavaScript Cake in PHP Symfony in PHP5 Seagull in PHP PHP on Trax another one in PHP MonoRail in .NET Wicket for Java Sails for Java Groovy on Rails Trails also in Java Seaside in Smalltalk
Ruby On Rails aujourd’hui
des raisons à tout ce bruit ?
Complexité
La plateforme J2EE souffre d’une complexité trop élevée pour la majorité des applications Web. Le ticket d’entrée est très élevé.
Design by Comittee => Don't Let Architecture Astronauts Scare You (Joel Spolsky Avril 2001)
Langages “Web”
• Perl :
Populaire, idéal pour experimenter rapidement. Difficultés de maintenance et lisibilité Langage de script HTML Manque de structure
• PHP
Contexte Web 2.0
• Regain d’intérêt pour les clients riches
Google map, gmail, ...
• Buzz autour d’Ajax
Version 1.0 Décembre 2005 Version 1.1 Mars 2006
Un framework d’une application et non pas issu de comités d’experts...
extrait
Pourquoi Ruby ?
• David Heinemeier Hansson était à la
recherche d’un langage productif pour une application de gestion de projets online. éxecution de codes durant la définition d’une classe, concision, expressivité, ...)
• L’esthétique est importante (blocks, • Meta-programming !
Ruby
est un langage complétement orienté-objet, très similaire à Smalltalk, créé par Yukihiro Matsumoto (Matz) autour de 1995.
Ruby
Typé dynamiquement
(par opposition à Java or C++) Contient les mêmes facilités trouvées dans les langages de script modernes comme Perl et Python.
Ruby
Open Source et disponible sur Windows, Mac OS X, Linux, ... La syntaxe est concise. Vraiment ! Closures (à la LISP) Expressions régulières *dans* le langage Très nombreuses bibliothèques disponibles
Rubismes
public class Hello { public static void main(String [] args){ for(int i=0; i<5; i++){ System.out.println("Hello"); } } }
Java
5.times do puts 'hello' end
Ruby
Rubismes
import java.util.*; ... Map hash = new Hashtable(); hash.put("un", new Integer(1)); hash.put("deux", new Integer(2)); int val = ((Integer)hash.get("deux")).intValue(); } }
Java
hash={"un"=>1, "deux"=>2} val=hash["deux"]
Ruby
Rubismes
# Blocks et expressions régulières a=["un", "deux", "trois", "quatre"] a.find_all{|m| m =~ /^.?u/} => ["un", "quatre"] # Swap a,b=b,a # Tableaux t=[1, 2, 3] a, b, c = t # a contient 1, b 2, c3
Rails grâce à Ruby
Invocation de méthodes dans la définition
Classe “ré-ouverte”
Enfin Ruby ?
Ron Jeffries (2002) What productivity jump do you see in the near future? Honestly I don't see one. I hope that the agile methods like XP will help teams to be more productive, and I hope that the world will learn that
languages like Ruby
those with static typing.
and Smalltalk are
dynamically typed more productive than
"I always knew one day Smalltalk would replace Java. I just didn't know it would be called Ruby." -- Kent Beck (attr.)
un peu plus de détails
Principaux composants
Text
Conventions versus Configuration http://mydomain.com/app/controller/method Une URL est mappée automatiquement vers le controleur et l’action correspondante. Le nom de la classe représentant le modèle est lié au nom de la table. ...
Aurevoir XML !
DRY Don’t Repeat Yourself Pour chaque information, une seule source.
Générateurs Résultats immédiats ! ;-) Le Scaffolding automatise intégralement la création d’une appli web très simple couplée à un modèle de tables existant.
Feedback immédiat !
Active Record Active Record Design Pattern : An object that wraps a row in a database table or view, encapsulates the database access, and adds domain logic on that data (Martin Fowler in PEAA)
Active Record
Convention
AJAX
• Librairies Javascript prototype, effects,
dragdrop and controls disponibles javascript en Ruby
• JavascriptHelper permet d’accéder
<%= link_to_remote(“Click me”,
:update => ‘mydiv’, :url => {:action=> :say_hello }) %>
Ce texte va changer
Qui prend le train ?
Risques liés au langage
Typé dynamiquement. Test Driven Development est un excellent remplacement à la pseudo-sécurité du typage fort. Nécessite cependant une période d’adaptation. Syntaxe La concision du langage est-elle un frein à la maintenabilité ? Performance Rarement un facteur bloquant.
Risques liés au contexte
Integration dans l’infrastructure existante des entreprises ? Capitalisation J2EE/PHP/... perdue ? Maintenance sur le long terme ? Masse critique ?
Adoption de RoR : plus rapide chez les langages Web
J2EE .NET
PHP Perl
Frameworks D’Entreprise
Langages “Web”
JSF Expert Group Member
David Geary
September 2005
At the outset, I didn't believe in the Rails gospel, but after actually using Rails for a couple hours—thanks, Dave Thomas, May I have another?—I was hooked. I couldn't stop programming with Rails...
Will Rails hit a tipping point and become widely adopted in the near future? I am certain of it; in fact, I
think the tipping has already begun. But, of course, only time will tell.
La philosophie derrière Ruby On Rails ?
L’Agilité
Ruby On
Framework MVC pour écrire des applications web
Productif
Conventions versus flexibilité et DRY Inclut support AJAX, web services, email-handling Encourage les Tests unitaires !
Ruby comme langage unique
Quelques liens
✓ http://www.rubyonrails.org
Le portail “officiel”
✓http://www.rubycentral.com/book/intro.html
Le livre sur Ruby
✓http://www.digitalmediaminute.com/article/
1816/top-ruby-on-rails-tutorials Une sélection de tutoriels en ligne.
Livres
A venir ...
...O’Reilly prépare 2 livres,
Assez discuté ! Quelques preuves avec une démo