Your SlideShare is downloading. ×
0
Java, Ruby & Rails
The Java platform <ul><li>“ It’s all about the Java Virtual Machine. That’s the integration hub.” Gosling, Sun Tech Days 2...
Why Ruby? <ul><li>Ubiquity </li></ul><ul><ul><li>C-Ruby (MRI) </li></ul></ul><ul><ul><li>JRuby </li></ul></ul><ul><ul><li>...
What is Ruby? <ul><li>Created by Yukihiro Matsumoto (Matz) in 1993 </li></ul><ul><li>“ A dynamic, open source programming ...
Ruby Characteristics <ul><li>Interpreted </li></ul><ul><li>Object oriented </li></ul><ul><ul><li>” myString”.upcase </li><...
Dynamic features <ul><li>Reopen existing classes </li></ul><ul><ul><li>redefine methods </li></ul></ul><ul><ul><li>“ monke...
Core Ruby tools <ul><li>(j)ruby </li></ul><ul><ul><li>the interpreter </li></ul></ul><ul><li>(j)irb </li></ul><ul><ul><li>...
Ruby anatomy <ul><li>Classes are CamelCased </li></ul><ul><li>Methods are under_scored </li></ul><ul><li>Generally no need...
Example
Ruby structure <ul><li>Modules </li></ul><ul><ul><li>Module::Class </li></ul></ul><ul><ul><li>Mix-ins interfaces with func...
Access modifiers <ul><li>private </li></ul><ul><ul><li>Private to this instance (might be a subclass) </li></ul></ul><ul><...
Testing <ul><li>Test::Unit::TestCase </li></ul><ul><ul><li>xUnit for ruby </li></ul></ul><ul><li>JRuby to test Java code? ...
Method signature - Java <ul><li>Method overloading </li></ul><ul><li>Number of method signatures increases fast if flexibi...
Method signature – PL/SQL <ul><li>Named parameters </li></ul><ul><li>Still have to change method signature too add paramet...
Method signature – Ruby <ul><li>Hash as optional parameter, not named parameters </li></ul><ul><li>No need to change metho...
More syntax examples <ul><li>=~  regexp matcher </li></ul><ul><ul><li>myString =~ /[0-9]+/ </li></ul></ul><ul><li>%w[]  St...
Ruby on Rails <ul><li>MVC Framework </li></ul><ul><li>“ Rails is the most well thought-out web development framework I’ve ...
RoR principles <ul><li>Opinionated software </li></ul><ul><li>Convention over configuration </li></ul><ul><li>DRY </li></u...
Ruby on Rails concepts <ul><li>rails <appname> generates skeleton  </li></ul><ul><li>routes.rb ~struts-config.xml </li></u...
RoR tools <ul><li>rails <appname> -d <database> </li></ul><ul><li>script/generate scaffold Person name:string </li></ul><u...
ActiveRecord O/R mapping <ul><li>Db migrations, dev, test, prod   </li></ul><ul><ul><li>up and down </li></ul></ul><ul><ul...
RoR model example <ul><li>class BlogPost < ActiveRecord::Base </li></ul><ul><li>belongs_to :author </li></ul><ul><li>has_m...
A/R Metaprogramming
A/R Metaprogramming
ActiveSupport::TestCase <ul><li>Inherits Test::Unit::TestCase </li></ul><ul><ul><li>Provides helpers, eg http calls </li><...
Jruby Motivation <ul><li>The JVM </li></ul><ul><li>Sneak into the enterprise </li></ul><ul><li>Reuse infrastructure </li><...
JRuby on Rails <ul><li>Develop as Java </li></ul><ul><ul><li>Netbeans </li></ul></ul><ul><ul><li>Eclipse </li></ul></ul><u...
Java integration <ul><li>include Java / require ”java” </li></ul><ul><li>include Java::JavaxSwing </li></ul><ul><li>includ...
IDE support <ul><li>Eclipse - DLTK (Dynamic Languages Toolkit) </li></ul><ul><ul><li>Eclipse foundation plugin </li></ul><...
Conclusion <ul><li>Ruby is sweet! </li></ul><ul><ul><li>Syntactic sugar </li></ul></ul><ul><ul><li>Fun to work with </li><...
Thank you and Namaste
Contact © Devoteam Consulting A/S. This document  is not  to be copied or reproduced in any way without the  express permi...
Upcoming SlideShare
Loading in...5
×

Java, Ruby & Rails

1,870

Published on

An introduciton to the ruby/jruby ecosystem. Also touching a bit on Rails. Presented internally for our Java consultants

Published in: Technology
0 Comments
2 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total Views
1,870
On Slideshare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
Downloads
27
Comments
0
Likes
2
Embeds 0
No embeds

No notes for slide
  • 15/01/10
  • Transcript of "Java, Ruby & Rails"

    1. 1. Java, Ruby & Rails
    2. 2. The Java platform <ul><li>“ It’s all about the Java Virtual Machine. That’s the integration hub.” Gosling, Sun Tech Days 2007 </li></ul><ul><li>Lots of new languages for the JVM </li></ul><ul><ul><li>Scala, Clojure, Groovy, Jython, (J)Ruby, JavaScript (Rhino), JavaFX </li></ul></ul><ul><li>The DaVinci Machine </li></ul><ul><ul><li>JSR-292 </li></ul></ul><ul><ul><li>Target JDK 7 </li></ul></ul><ul><ul><li>invokedynamic instruction </li></ul></ul>
    3. 3. Why Ruby? <ul><li>Ubiquity </li></ul><ul><ul><li>C-Ruby (MRI) </li></ul></ul><ul><ul><li>JRuby </li></ul></ul><ul><ul><li>IronRuby (.NET) </li></ul></ul><ul><ul><li>Rubinius, Maglev, YARV </li></ul></ul><ul><li>Scripting </li></ul><ul><ul><li>java </li></ul></ul><ul><ul><li>shebang/backticks </li></ul></ul><ul><li>Rails Framework </li></ul><ul><li>Support from Sun, Thoughtworks </li></ul><ul><li>Most likely to succeed? </li></ul>
    4. 4. What is Ruby? <ul><li>Created by Yukihiro Matsumoto (Matz) in 1993 </li></ul><ul><li>“ 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.” </li></ul><ul><li>The principle of least surprise </li></ul><ul><li>Runtime efficiency not a top priority </li></ul><ul><li>“ More powerful than Perl, more OO </li></ul><ul><li>than Python” </li></ul><ul><li>MRI is the specification </li></ul><ul><ul><li>RubySpec is in the works </li></ul></ul>
    5. 5. Ruby Characteristics <ul><li>Interpreted </li></ul><ul><li>Object oriented </li></ul><ul><ul><li>” myString”.upcase </li></ul></ul><ul><ul><li>2_500.+2 </li></ul></ul><ul><ul><li>44.modulo 5 </li></ul></ul><ul><li>Reflective </li></ul><ul><li>Garbage collected </li></ul><ul><li>Duck typing </li></ul><ul><ul><li>If it walks like a duck and quacks </li></ul></ul><ul><ul><li>like a duck it must be a duck </li></ul></ul>
    6. 6. Dynamic features <ul><li>Reopen existing classes </li></ul><ul><ul><li>redefine methods </li></ul></ul><ul><ul><li>“ monkeypatching” </li></ul></ul><ul><li>Meta-programming </li></ul><ul><li>Method aliasing </li></ul><ul><ul><li>alias_method :validates_size_of , :validates_length_of </li></ul></ul><ul><li>method_missing </li></ul><ul><ul><li>Eg provide a generic sort method sort_by_x </li></ul></ul><ul><li>Closures </li></ul>
    7. 7. Core Ruby tools <ul><li>(j)ruby </li></ul><ul><ul><li>the interpreter </li></ul></ul><ul><li>(j)irb </li></ul><ul><ul><li>interactive ruby, ~readline support, tab completion </li></ul></ul><ul><li>ri </li></ul><ul><ul><li>ruby interactive ~man pages </li></ul></ul><ul><li>rdoc </li></ul><ul><ul><li>html doc ~javadoc </li></ul></ul><ul><li>rake </li></ul><ul><ul><li>ant/make for ruby </li></ul></ul><ul><li>gem </li></ul><ul><ul><li>package manager </li></ul></ul>
    8. 8. Ruby anatomy <ul><li>Classes are CamelCased </li></ul><ul><li>Methods are under_scored </li></ul><ul><li>Generally no need for curly braces, parentheses, semicolon or return statement </li></ul><ul><ul><li>code more compact </li></ul></ul><ul><li>Comments </li></ul><ul><ul><li>=begin block comment =end </li></ul></ul><ul><ul><li># line comment </li></ul></ul><ul><li>Variables </li></ul><ul><ul><li>Constant </li></ul></ul><ul><ul><li>@@classVariable </li></ul></ul><ul><ul><li>@instanceVariable </li></ul></ul><ul><ul><li>localVariable </li></ul></ul><ul><li>:symbol </li></ul>
    9. 9. Example
    10. 10. Ruby structure <ul><li>Modules </li></ul><ul><ul><li>Module::Class </li></ul></ul><ul><ul><li>Mix-ins interfaces with functionality </li></ul></ul><ul><ul><li>Namespace </li></ul></ul><ul><li>Subclass < Superclass </li></ul><ul><ul><li>Single inheritance only </li></ul></ul><ul><li>Files are named *.rb </li></ul><ul><ul><li>can hold many classes </li></ul></ul><ul><li>require ’file’ </li></ul><ul><li>include Module </li></ul><ul><li>searches $LOAD_PATH </li></ul>
    11. 11. Access modifiers <ul><li>private </li></ul><ul><ul><li>Private to this instance (might be a subclass) </li></ul></ul><ul><ul><li>You can’t specify the receiver, not even self </li></ul></ul><ul><li>protected </li></ul><ul><ul><li>Can be called by instances of the same class </li></ul></ul><ul><ul><li>Eg use for comparators </li></ul></ul><ul><li>public </li></ul><ul><ul><li>FFA </li></ul></ul><ul><li>private :method or private (until end) </li></ul><ul><li>The modifiers are methods, not keywords </li></ul>
    12. 12. Testing <ul><li>Test::Unit::TestCase </li></ul><ul><ul><li>xUnit for ruby </li></ul></ul><ul><li>JRuby to test Java code? </li></ul><ul><ul><li>JtestR to invoke </li></ul></ul><ul><ul><li>Code coverage </li></ul></ul><ul><ul><li>might be an issue </li></ul></ul><ul><li>RSpec </li></ul><ul><ul><li>BDD </li></ul></ul>
    13. 13. Method signature - Java <ul><li>Method overloading </li></ul><ul><li>Number of method signatures increases fast if flexibility is wanted </li></ul>
    14. 14. Method signature – PL/SQL <ul><li>Named parameters </li></ul><ul><li>Still have to change method signature too add parameters </li></ul>
    15. 15. Method signature – Ruby <ul><li>Hash as optional parameter, not named parameters </li></ul><ul><li>No need to change method signature </li></ul><ul><li>Hash doesn’t need curly braces </li></ul><ul><li>Method overload unavailable </li></ul><ul><li>*args => arguments array </li></ul>
    16. 16. More syntax examples <ul><li>=~ regexp matcher </li></ul><ul><ul><li>myString =~ /[0-9]+/ </li></ul></ul><ul><li>%w[] String array </li></ul><ul><ul><li>myArray = %w[ruby java haskell] </li></ul></ul><ul><li>{||} or do || end closures </li></ul><ul><ul><li>(and embedded variables) </li></ul></ul><ul><ul><li>myHash.each { |k, v| puts k+’ is ‘+v } </li></ul></ul><ul><ul><li>myHash.each do |key, val| </li></ul></ul><ul><ul><li>puts “#{key} is #{val}” </li></ul></ul><ul><ul><li>end </li></ul></ul>
    17. 17. Ruby on Rails <ul><li>MVC Framework </li></ul><ul><li>“ 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 </li></ul><ul><li>“ Rails is the killer app for Ruby.” Yukihiro Matsumoto, Creator of Ruby </li></ul>
    18. 18. RoR principles <ul><li>Opinionated software </li></ul><ul><li>Convention over configuration </li></ul><ul><li>DRY </li></ul><ul><li>REST </li></ul><ul><ul><li>GET /products #get all </li></ul></ul><ul><ul><li>GET /products/42 #get id=42 </li></ul></ul><ul><ul><li>POST /products #create </li></ul></ul><ul><li>Fast feedback loop </li></ul><ul><ul><li>just reload, no compile </li></ul></ul><ul><li>Rails itself is a gem </li></ul>
    19. 19. Ruby on Rails concepts <ul><li>rails <appname> generates skeleton </li></ul><ul><li>routes.rb ~struts-config.xml </li></ul><ul><li>*.html.erb ~JSP </li></ul><ul><li>Generators </li></ul><ul><ul><li>Generates model, view, controller, tests </li></ul></ul><ul><li>Partials (html.erb snippets) </li></ul><ul><li>Plugins (instead of gems) </li></ul><ul><li>Default database is sqlite3 (via JDBC for JRuby) </li></ul><ul><li>Filters (defined in controller) </li></ul>
    20. 20. RoR tools <ul><li>rails <appname> -d <database> </li></ul><ul><li>script/generate scaffold Person name:string </li></ul><ul><li>script/server </li></ul><ul><li>script/console </li></ul><ul><ul><li>irb with access to the application </li></ul></ul><ul><li>script/dbconsole </li></ul><ul><ul><li>enter SQL </li></ul></ul><ul><li>script/server –debugger </li></ul><ul><ul><li>debugger in the code drops to console </li></ul></ul>
    21. 21. ActiveRecord O/R mapping <ul><li>Db migrations, dev, test, prod </li></ul><ul><ul><li>up and down </li></ul></ul><ul><ul><li>database agnostic </li></ul></ul><ul><ul><li>rake db:migrate </li></ul></ul><ul><ul><li>config/database.yml </li></ul></ul><ul><li>Opinionated software </li></ul><ul><ul><li>primary key named id </li></ul></ul><ul><ul><li>pluralization class Book => db books </li></ul></ul>
    22. 22. RoR model example <ul><li>class BlogPost < ActiveRecord::Base </li></ul><ul><li>belongs_to :author </li></ul><ul><li>has_many :comments </li></ul><ul><li>validates_presence_of :title </li></ul><ul><li>end </li></ul><ul><li>comments table needs blog_post_id </li></ul><ul><li>blog_posts table needs author_id </li></ul><ul><li>New post need title field </li></ul>
    23. 23. A/R Metaprogramming
    24. 24. A/R Metaprogramming
    25. 25. ActiveSupport::TestCase <ul><li>Inherits Test::Unit::TestCase </li></ul><ul><ul><li>Provides helpers, eg http calls </li></ul></ul><ul><li><app>/test </li></ul><ul><ul><li>functional </li></ul></ul><ul><ul><li>integration </li></ul></ul><ul><ul><li>unit </li></ul></ul><ul><li>fixtures/fixtures.yml </li></ul><ul><li>rake </li></ul>
    26. 26. Jruby Motivation <ul><li>The JVM </li></ul><ul><li>Sneak into the enterprise </li></ul><ul><li>Reuse infrastructure </li></ul><ul><li>Integrate with Java applications </li></ul><ul><li>Performance and scalability </li></ul>
    27. 27. JRuby on Rails <ul><li>Develop as Java </li></ul><ul><ul><li>Netbeans </li></ul></ul><ul><ul><li>Eclipse </li></ul></ul><ul><li>Integrate with Java </li></ul><ul><ul><li>Call EJBs </li></ul></ul><ul><ul><li>Use JMS queues </li></ul></ul><ul><ul><li>Use JNDI </li></ul></ul><ul><ul><li>Use JAAS </li></ul></ul><ul><li>Deploy on Java </li></ul><ul><ul><li>As *.war using warbler </li></ul></ul><ul><ul><li>Rails deployer (JBoss, Glassfish) </li></ul></ul>
    28. 28. Java integration <ul><li>include Java / require ”java” </li></ul><ul><li>include Java::JavaxSwing </li></ul><ul><li>include_class &quot;javax.naming.InitialContext” </li></ul><ul><li>require ”path/to/my.jar” </li></ul><ul><li>prefix javaclasses to avoid name collisions </li></ul><ul><ul><li>String => JString </li></ul></ul><ul><ul><li>include_class 'java.lang.String’ {|package,name| &quot;J#{name}&quot; } </li></ul></ul><ul><li>Method alias </li></ul><ul><ul><li>System.currentTimeMillis => System.current_time_millis </li></ul></ul><ul><li>Getters and setters behaves like attr_accessor fields </li></ul>
    29. 29. IDE support <ul><li>Eclipse - DLTK (Dynamic Languages Toolkit) </li></ul><ul><ul><li>Eclipse foundation plugin </li></ul></ul><ul><ul><li>Support for TCL, Python, Ruby </li></ul></ul><ul><ul><li>No Rails </li></ul></ul><ul><li>Eclipse - Aptana RadRails </li></ul><ul><ul><li>RDT Ruby Editor </li></ul></ul><ul><ul><li>Rails support generators/scripts </li></ul></ul><ul><ul><li>Visual debug </li></ul></ul><ul><ul><li>Testrunner </li></ul></ul><ul><li>Netbeans </li></ul><ul><ul><li>Official plugin </li></ul></ul>
    30. 30. Conclusion <ul><li>Ruby is sweet! </li></ul><ul><ul><li>Syntactic sugar </li></ul></ul><ul><ul><li>Fun to work with </li></ul></ul><ul><li>Ruby is useful! </li></ul><ul><ul><li>Integrate with Java </li></ul></ul><ul><ul><li>OO scripting </li></ul></ul><ul><ul><li>Rails is probably fast enough </li></ul></ul><ul><li>Ruby is hard! </li></ul><ul><ul><li>“ Good programmers become better, bad programmers become worse” </li></ul></ul><ul><ul><li>Test, test, test </li></ul></ul>
    31. 31. Thank you and Namaste
    32. 32. 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 +46 (0)733-812135 Phone: [email_address] E-mail: Address Peter Sönnergren Person: CONTACT Author: Date: # ID: DOCUMENT
    1. A particular slide catching your eye?

      Clipping is a handy way to collect important slides you want to go back to later.

    ×