9. Duck-Typing
def area(width = 10, height = 2 * width)
width * height
end
p area # => 200
p area 5 # => 50
p area 5, 20 # => 100
p area "10", 4 # => ?
10. Duck-Typing
p area "10", 4 # => "10101010"
# From Ruby API docs:
# String#*(num)
#
# Returns a new String containing num copies of
# the receiver.
#
# "Ho! " * 3 #=> "Ho! Ho! Ho! "
area true, false
# => NoMethodError: undefined method `*' for
# true:TrueClass
12. Blocks
list = [1, 2, 3, 4]
list.each {|n| puts n }
Ruby
list.each do |n|
puts n
end
List<Integer> list = Arrays.asList(1, 2, 3, 4);
Java for (Integer n : list) {
System.out.println(n);
}
13. Blocks
File.open(__FILE__) do |file|
file.each_line do |line|
Ruby puts line
end
end
BufferedReader file =
new BufferedReader(new FileReader("Blocks.java"));
try {
String line;
while ((line = buf.readLine()) != null) {
Java System.out.println(line);
}
} finally {
file.close();
}
14. Open Classes
msg = "Scramble this so you can't read it!"
msg.rot13!
# => NoMethodError: undefined method `rot13!' for
# "Scramble this so you can't read it!":String
15. Open Classes
class String
def rot13!
0.upto(length - 1) do |i|
case self[i]
when ?a..?z
self[i] = ?a + ((self[i] - ?a) + 13) % 26
when ?A..?Z
self[i] = ?A + ((self[i] - ?A) + 13) % 26
end
end
self
end
end
16. Open Classes
puts msg.rot13!
# => "Fpenzoyr guvf fb lbh pna'g ernq vg!"
puts msg.rot13!
# => "Scramble this so you can't read it!"
21. Convention over Configuration
URL GET /people
resources :people
Routing #=> people#index
# app/controllers/people_controller.rb
class PeopleController < ApplicationController
def index
Controller @people = Person.all
end
end
# app/models/person.rb
Model class Person < ActiveRecord::Base
end
View app/views/people/index.html.erb
22. Defaults with Choices
Default Alternatives
DataMapper, MongoMapper,
ORM ActiveRecord Sequel, Any object with
ActiveModel
HAML, XML Builder,
View Templates ERb Markaby, RedCloth (Textile),
BlueCloth (Markdown)
JavaScript
Prototype jQuery
Framework
MySQL, PostgreSQL,
Database SQLite3 Oracle, more via JRuby +
JDBC
Test Framework Test::Unit RSpec, Cucumber
26. New Application
$ rails new coolapp -m http://jruby.org
create
create README
create Rakefile
...
27. Dependencies with Bundler
$ cd coolapp
$ bundle install
Fetching source index for http://rubygems.org/
Using rake (0.8.7)
Using abstract (1.0.0)
...
Using rails (3.0.3)
Your bundle is complete!
30. Start Dev Server
$ rails server
=> Booting WEBrick
=> Rails 3.0.3 application starting in development on http://0.0.0.0:3000
=> Call with -d to detach
=> Ctrl-C to shutdown server
[2010-12-13 20:11:28] INFO WEBrick 1.3.1
[2010-12-13 20:11:28] INFO ruby 1.8.7 (2010-12-10) [java]
[2010-12-13 20:11:28] INFO WEBrick::HTTPServer#start: pid=21022 port=3000
32. Controller
class PeopleController < ApplicationController
# GET /people
# GET /people.xml
def index
@people = Person.all
respond_to do |format|
format.html # index.html.erb
format.xml { render :xml => @people }
end
end
def show; end
def new; end
def edit; end
def create; end
def update; end
def destroy; end
end
54. Warbler
INSTALL gem install warbler
• Create a Java EE .war file from a Rails application
• “Looks like Java” to the ops staff
deploy
Rails
warble app.war to java
app
appserver
55. JRuby Deployment
Ruby servers WAR files Cloud
WEBrick GlassFish EY AppCloud
Trinidad Tomcat AppEngine
TorqueBox JBoss AWS EB
64. Metaphor Use Ruby, JRuby, and Rails to...
Sagrada • Build new facade faster
Familia • Scaffolding during refactoring
Ryugyong
• Revive a project with a new face
Hotel
Seismic • Reinforce business rules with a DSL
retrofit • Harden security
Szkieletor • Find novel uses for abandoned code