6. ...computer engineers, focus on the machines. They think, "By
doing this, the machine will run faster. By doing this, the
machine will run more effectively..." They are focusing on
machines. But in fact we need to focus on humans, on how
humans care about doing programming...
We are the masters. They are the slaves.
7. "I hope to see Ruby help every programmer
in the world to be productive, and to enjoy
programming, and to be happy. That is the
primary purpose of Ruby language."
35. class Person
def walk
"walking..."
end
end
juan = Person.new
juan.walk # => "walking..."
class Person
def eat
"eating..."
end
end
juan.eat # => "eating..."
52. module Walkable
def walk
puts "I'm walking"
end
end
module Runnable
def run
puts "I'm running"
end
end
class Person
include Walkable
include Runnable
end
joe = Person.new
joe.walk # => I'm walking
joe.run # => I'm running
53. module Summable
def sum(a, b)
a + b
end
end
class Number
extend Summable
end
Number.sum 2, 4 # => 6
57. 3.times do
puts "hey"
end
3.times { puts "hey" }
58. 3.times do
puts "hey"
end
class Fixnum
def times
i = 0
while i < self
yield
i += 1
end
end
end
59. 3.times do
puts "hey"
end
class Fixnum
def times(&block)
i = 0
while i < self
block.call
i += 1
end
end
end
60. 3.times do 3.times do
puts "hey" puts "hey"
end end
class Fixnum class Fixnum
def times def times(&block)
i = 0 i = 0
while i < self while i < self
yield block.call
i += 1 i += 1
end end
end end
end end
61. 3.times do |i|
puts i
end
class Fixnum
def times
i = 0
while i < self
yield(i)
i += 1
end
end
end
93. class CreateUsers < ActiveRecord::Migration
def self.up
create_table :users do |t|
t.string :name
t.string :email
t.timestamps
end
end
def self.down
drop_table :users
end
end
94. $ rails g model Profile user_id:integer about:text
97. class CreateProfiles < ActiveRecord::Migration
def self.up
create_table :profiles do |t|
t.integer :user_id
t.text :about
t.timestamps
end
end
def self.down
drop_table :profiles
end
end
122. @user = User.find(3)
# => SELECT * FROM users WHERE id = 3;
@user = User.find_by_name(“Joe”)
# => SELECT * FROM users WHERE name = ‘Joe’ LIMIT 1;
@user = User.first
# => SELECT * FROM users LIMIT 1;
123. @users = User.all
# => SELECT * FROM users;
@users = User.where(:name => “John”)
# => SELECT * FROM users WHERE name = ‘John’;
@users = User.where(“name LIKE ?”, “Jo%”)
# => SELECT * FROM users WHERE name LIKE ‘Jo%’;
124. @users = User.order(“created_at DESC”)
# => SELECT * FROM users ORDER BY created_at DESC;
@users = User.joins(:profile).where(“profiles.about IS NULL”)
# => SELECT * FROM users LEFT OUTER JOIN profiles ON users.id =
profiles.user_id WHERE profiles.about IS NULL;
135. Isshoni::Application.routes.draw do
get '/users' => 'users#index'
get '/users/:id' => 'users#show'
get '/users/new' => 'users#new'
post '/users' => 'users#create'
get '/users/:id/edit' => 'users#edit'
put '/users/:id' => 'users#update'
delete '/users/:id' => 'users#destroy'
end
146. Isshoni::Application.routes.draw do
get '/users' => 'users#index'
get '/users/:id' => 'users#show'
get '/users/new' => 'users#new'
post '/users' => 'users#create'
get '/users/:id/edit' => 'users#edit'
put '/users/:id' => 'users#update'
delete '/users/:id' => 'users#destroy'
end
149. class UsersController < ApplicationController
def index
end
def show
end
def new
end
def create
end
def edit
end
def update
end
def destroy
end
end
150. class UsersController < ApplicationController
# GET /users
def index
@users = User.all
end
end
151. class UsersController < ApplicationController
# GET /users/:id
def show
@user = User.find(params[:id])
end
end
152. class UsersController < ApplicationController
# GET /users/new
def new
@user = User.new
end
end
153. class UsersController < ApplicationController
# POST /users
def create
@user = User.new(params[:user])
if @user.save
redirect_to users_path
else
render :new
end
end
end
154. class UsersController < ApplicationController
# GET /users/:id/edit
def edit
@user = User.find(params[:id])
end
end
155. class UsersController < ApplicationController
# PUT /users/:id
def update
@user = User.find(params[:id])
if @user.update_attributes(params[:user])
redirect_to user_path(@user)
else
render :edit
end
end
end
156. class UsersController < ApplicationController
# DELETE /users/:id
def destroy
@user = User.find(params[:id])
@user.destroy
redirect_to users_path
end
end
157. class UsersController < ApplicationController
# GET /users
def index
@users = User.all
end
# GET /users/:id/edit
# GET /users/:id
def edit
def show
@user = User.find(params[:id])
@user = User.find(params[:id])
end
end
# PUT /users/:id
# GET /users/new
def update
def new
@user = User.find(params[:id])
@user = User.new
if
end
@user.update_attributes(params[:user])
redirect_to user_path(@user)
# POST /users
else
def create
render :edit
@user = User.new(params[:user])
end
if @user.save
end
redirect_to users_path
else
# DELETE /users/:id
render :new
def destroy
end
@user = User.find(params[:id])
end
@user.destroy
redirect_to users_path
end
end
159. class UsersController < ApplicationController
before_filter :get_user, :only => [:show, :edit, :update, :destroy]
def show
end
def edit
end
def update
if @user.update_attributes(params[:user])
redirect_to user_path(@user)
else
render :edit
end
end
def destroy
@user.destroy
redirect_to users_path
end
private
def get_user
@user = User.find(params[:id])
end
end
192. class UserMailer < ActionMailer::Base
default :from => "no-reply@foobar.com"
def password_reset_instructions(user)
@user = user
mail :to => @user.email, :subject => "Password Reset"
end
end
196. class UsersController < ApplicationController
def create
@user = User.new(params[:user])
if @user.save
redirect_to users_path
else
render :new
end
end
end
204. # params[:username] = "nick' OR 1 = 1 OR username LIKE '%"
@users = User.where("username LIKE '%#{params[:username]}%'")
# SELECT * FROM users WHERE username LIKE '%nick' OR 1 = 1 OR username LIKE '%';
205. # params[:username] = "nick' OR 1 = 1 OR username LIKE '%"
@users = User.where("username LIKE ?", "%#{params[:username]}%")
# SELECT * FROM users WHERE username LIKE 'nick' OR 1 = 1 OR username LIKE '%';
212. Started POST "/sessions" for 127.0.0.1 at Sat Jun 25 19:43:44 -0500 2011
Processing by SessionsController#create as HTML
Parameters: {"password"=>"123456", "email"=>"foo@bar.com"}
SQL (0.5ms) SHOW TABLES
Account Load (0.2ms) SELECT `accounts`.* FROM `accounts` WHERE
`accounts`.`email` = 'foo@bar.com' LIMIT 1
Completed 200 OK in 274ms (Views: 66.3ms | ActiveRecord: 0.7ms)
214. module Deli
class Application < Rails::Application
config.filter_parameters += [:password]
end
end
215. Started POST "/sessions" for 127.0.0.1 at Sat Jun 25 19:43:44 -0500 2011
Processing by SessionsController#create as HTML
Parameters: {"password"=>"[FILTERED]", "email"=>"foo@bar.com"}
SQL (0.5ms) SHOW TABLES
Account Load (0.2ms) SELECT `accounts`.* FROM `accounts` WHERE
`accounts`.`email` = 'foo@bar.com' LIMIT 1
Completed 200 OK in 274ms (Views: 66.3ms | ActiveRecord: 0.7ms)
225. class ApplicationController < ActionController::Base
before_filter :set_language
private
def set_language
I18n.locale = params[:lang].to_sym if params[:lang]
end
end