Click here to load reader

Java, Ruby & Rails

  • View
    2.905

  • Download
    3

Embed Size (px)

DESCRIPTION

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

Text of Java, Ruby & Rails

  • Java, Ruby & Rails
  • The Java platform
    • Its all about the Java Virtual Machine. Thats the integration hub. Gosling, Sun Tech Days 2007
    • Lots of new languages for the JVM
      • Scala, Clojure, Groovy, Jython, (J)Ruby, JavaScript (Rhino), JavaFX
    • The DaVinci Machine
      • JSR-292
      • Target JDK 7
      • invokedynamic instruction
  • Why Ruby?
    • Ubiquity
      • C-Ruby (MRI)
      • JRuby
      • IronRuby (.NET)
      • Rubinius, Maglev, YARV
    • Scripting
      • java
      • shebang/backticks
    • Rails Framework
    • Support from Sun, Thoughtworks
    • Most likely to succeed?
  • 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 surprise
    • Runtime efficiency not a top priority
    • More powerful than Perl, more OO
    • than Python
    • MRI is the specification
      • RubySpec is in the works
  • Ruby Characteristics
    • Interpreted
    • Object oriented
      • myString.upcase
      • 2_500.+2
      • 44.modulo 5
    • Reflective
    • Garbage collected
    • Duck typing
      • If it walks like a duck and quacks
      • like a duck it must be a duck
  • Dynamic features
    • Reopen existing classes
      • redefine methods
      • monkeypatching
    • Meta-programming
    • Method aliasing
      • alias_method :validates_size_of , :validates_length_of
    • method_missing
      • Eg provide a generic sort method sort_by_x
    • Closures
  • 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
  • Ruby anatomy
    • Classes are CamelCased
    • Methods are under_scored
    • Generally 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
  • Example
  • 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 Module
    • searches $LOAD_PATH
  • Access modifiers
    • private
      • Private to this instance (might be a subclass)
      • You cant 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
  • Testing
    • Test::Unit::TestCase
      • xUnit for ruby
    • JRuby to test Java code?
      • JtestR to invoke
      • Code coverage
      • might be an issue
    • RSpec
      • BDD
  • Method signature - Java
    • Method overloading
    • Number of method signatures increases fast if flexibility is wanted
  • Method signature PL/SQL
    • Named parameters
    • Still have to change method signature too add parameters
  • Method signature Ruby
    • Hash as optional parameter, not named parameters
    • No need to change method signature
    • Hash doesnt need curly braces
    • Method overload unavailable
    • *args => arguments array
  • 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
  • Ruby on Rails
    • MVC Framework
    • Rails is the most well thought-out web development framework Ive ever used. And thats in a decade of doing web applications for a living. Ive 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
  • RoR principles
    • Opinionated software
    • Convention over configuration
    • DRY
    • REST
      • 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
  • Ruby on Rails concepts
    • rails generates skeleton
    • routes.rb ~struts-config.xml
    • *.html.erb ~JSP
    • Generators
      • 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)
  • RoR tools
    • rails -d
    • script/generate scaffold Person name:string
    • script/server
    • script/console
      • irb with access to the application
    • script/dbconsole
      • enter SQL
    • script/server debugger
      • debugger in the code drops to console
  • 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
  • RoR model example
    • class BlogPost < ActiveRecord::Base
    • belongs_to :author
    • has_many :comments
    • validates_presence_of :title
    • end
    • comments table needs blog_post_id
    • blog_posts table needs author_id
    • New post need title field