3. Ruby
Everything is an object Dynamic Typing
true.class # TrueClass class Foo

nil.class # NilClass def initialize(x, y)

@x = x
@y = y
end
end
class Foo2
end
Foo.new(2, Foo2.new)
4. Ruby
class Foo class Bar
# Parenthesis can be omitted # Use ! if you change self
def method def method!
puts "Hello World" end
end
# Use ? if you return a boolean
# Default params def method?
def method2(x = 2) end
puts x
end # Only conventions
end
# Operator overload
def +(x)
end
end
5. Ruby
"hello".class # String
:hello.class # Symbol a = "Hello"
b = "Hello"
# Convention
# Use string if you plan to a.equal? b # false
compute text
x = :hello
# Use symbols if you want to y = :hello
define or/and set a
behaviour which is not x.equal? y # true
expected to change
7. Ruby
x = [1,4,5,2,5,8,10]
x.sort # returns a copy of x sorted
[1,2,4,5,5,8,10]
x.sort! # modifies self
x.map{ |i| i + 4 } # [5,6,8,9,9,12,14]
x.map! do |i|
i + 4
end # [5,6,8,9,9,12,14]
8. Ruby
Monkey Patching
class String
def +()
# override string default + operator
end
end
“With great power comes great responsability”
Uncle Ben, Amazing Spiderman nº1
13. • Don’t Repeat Yourself
• Increased productivity through
conventions. Ex.: following a pattern for
foreign key columns.
• Take advantage of singular and plural word
meanings
23. pcunha:Blog$ rails server
=> Booting WEBrick
=> Rails 3.0.7 application starting in
development on http://0.0.0.0:3000
=> Call with -d to detach
=> Ctrl-C to shutdown server
32. rake db:create
rake db:migrate
rake db:migrate:redo
rake db:rollback
blog_db.schema_migrations
- keeps the version number of all
migrations already runned
34. # app/models/post.rb
class Post < ActiveRecord::Base
has_many :comments
end
# app/models/comment.rb
class Comment < ActiveRecord::Base
belongs_to :post
end
Post.all
Post.find(1).comments
Comments.find(1).post
Post.order(:created_at)
Post.limit(5).offset(2)
39. # app/controllers/posts_controller.rb
class PostsController < ApplicationController
# GET /posts
def index ...
# GET /posts/1
def show ...
# GET /posts/new
def new ...
# GET /posts/1/edit
def edit ...
# POST /posts
def create ...
# PUT /posts/1
def update ...
# DELETE /posts/1
def destroy ...
end
Generated with scaffold
40. # POST /posts
# POST /posts.xml
def create
@post = Post.new(params[:post])
respond_to do |format|
if @post.save
format.html { redirect_to(@post, :notice => 'Post was
successfully created.') }
format.xml { render :xml => @post, :status
=> :created, :location => @post }
else
format.html { render :action => "new" }
format.xml { render :xml => @post.errors, :status
=> :unprocessable_entity }
end
end
end
41. def index
@posts = Post.all
respond_to do |format|
format.html # index.html.erb
format.xml { render :xml => @posts }
end
end
pcunha:Blog$ curl http://localhost:3000/posts.xml
<?xml version="1.0" encoding="UTF-8"?>
<posts type="array">
<post>
<created-at type="datetime">2011-07-15T13:39:51Z</created-at>
<body>This is the body of the first post</body>
<title>The first very post of this blog</title>
<updated-at type="datetime">2011-07-15T13:39:51Z</updated-at>
<id type="integer">1</id>
</post>
</posts>
45. Rails builds the route for you
link_to 'Show', @post # GET posts/@post.id
form_for(@post)
if @post.new_record?
POST /posts
else
PUT /posts/@post.id
end
50. Improve user experience by not having the
whole page reload when submitting a form
or simple pagination link
Also save resources used (SQL queries,
memory, more bandwidth usage,... etc)
57. # app/controllers/comments_controller.rb
class CommentsController < ApplicationController
def create
@post = Post.find(params[:post_id])
@comment = @post.comments.new(params[:comment])
respond_to do |format|
if @comment.save
format.html { redirect_to(@post, :notice => 'Comment was
successfully created.') }
format.js
else
format.html { render :action => "new" }
format.js
end
end
end
end
58. # app/views/comments/create.js.erb
//Dump javascript here!
document.getElementById...
Notice:
- create.js.erb
- writing native javascript is not optimal:
1. You will forget something about IE
2. We are at 21st Century
3. Lots of good frameworks
59. Rails 2.X and 3.0.X
- Prototype JS Framework as default
Rails 3.1 (released 2011)
- jQuery JS Framework as default