contagion的ruby/rails投影片

49
OSDC.TW 2006 RUBY on

Upload: cfc

Post on 27-Jan-2015

107 views

Category:

Technology


2 download

DESCRIPTION

有點舊了:P

TRANSCRIPT

Page 1: Contagion的Ruby/Rails投影片

OSDC.TW 2006

RUBY on

Page 2: Contagion的Ruby/Rails投影片

contagious

Page 3: Contagion的Ruby/Rails投影片

How Hot is RoR

Page 4: Contagion的Ruby/Rails投影片

0

112,500

225,000

337,500

450,000

2004 July 2004 Dec. 2005 Oct. 2006 April

over 400,000 downloads

Page 5: Contagion的Ruby/Rails投影片

• 400 seats, sold out in one week

• extra 150 seats, sold out in 24 hours

Page 6: Contagion的Ruby/Rails投影片

David Heinemeier

Hansson

Page 7: Contagion的Ruby/Rails投影片
Page 8: Contagion的Ruby/Rails投影片
Page 10: Contagion的Ruby/Rails投影片
Page 11: Contagion的Ruby/Rails投影片

RoR is so HOT Everybody wants to

clone it

Page 12: Contagion的Ruby/Rails投影片
Page 13: Contagion的Ruby/Rails投影片

What exactly is Ruby On Rails

Page 14: Contagion的Ruby/Rails投影片

Ruby on Rails is an open-source web

framework

Page 15: Contagion的Ruby/Rails投影片

optimized for programmer

happiness

Page 16: Contagion的Ruby/Rails投影片

What Makes Programmer Happy?

•Simplicity

• Easy to learn

•Easy to Write

•Well organized

•Easy to Maintain

Page 17: Contagion的Ruby/Rails投影片

SIMPLEEasy to Learn and Write

No separation between Business Logic and Display Logic

Page 18: Contagion的Ruby/Rails投影片

Corporate ApprovalWell Structured

Steep Learn CurveComplex Configuration

No fast turnaround

Page 19: Contagion的Ruby/Rails投影片

Finding the middle ground with

Page 20: Contagion的Ruby/Rails投影片

Full Stack of MVC

Page 21: Contagion的Ruby/Rails投影片

Real World Usage

Page 22: Contagion的Ruby/Rails投影片

One Language - Ruby

•Model - View - Controller

•Database Schema generation

• Javascript generation

•XML generation

•Makefile

Page 23: Contagion的Ruby/Rails投影片

Convention Over

Configuration

Page 24: Contagion的Ruby/Rails投影片

CREATE TABLE orders ( id INTEGER PRIMARY KEY AUTO_INCREMENT, order_date TIMESTAMP NOT NULL, price_total DOUBLE NOT NULL);

CREATE TABLE products ( id INTEGER PRIMARY KEY AUTO_INCREMENT, name VARCHAR(80) NOT NULL, price DOUBLE NOT NULL);

CREATE TABLE order_items ( id INTEGER PRIMARY KEY AUTO_INCREMENT, order_id INTEGER NOT NULL, product_id INTEGER NOT NULL, amount INTEGER NOT NULL, price DOUBLE NOT NULL);

An example from a

Hibernate tutorial

Page 25: Contagion的Ruby/Rails投影片

public class Order{ private Integer id; private Date date; private double priceTotal; private Set orderItems = new HashSet();

public Order() { this.date = new Date(); } public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public Date getDate() { return date; } public void setDate(date) { this.date = date; } public void addProduct(Product p, int amount) { OrderItem orderItem = new OrderItem(this, p, amount); this.priceTotal = this.priceTotal + p.getPrice() * amount; this.orderItems.add(orderItem); }}

Java Object Model:Order

Page 26: Contagion的Ruby/Rails投影片

<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD//EN" "http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd"><hibernate-mapping> <class name="test.hibernate.Order" table="orders"> <id name="id" type="string" unsaved-value="null" > <column name="id" sql-type="integer" not-null="true"/> <generator class="native"/> </id> <property name="date"> <column name="order_date" sql-type="datetime" not-null="true"/> </property> <property name="priceTotal"> <column name="price_total" sql-type="double" not-null="true"/> </property>

<set name="orderItems" table="order_items" inverse="true" cascade="all"> <key column="order_id" /> <one-to-many class="test.hibernate.OrderItem" /> </set> </class></hibernate-mapping>

Java Database Mapping:Order

Page 27: Contagion的Ruby/Rails投影片

class Order < ActiveRecord::Base has_many :order_items

def add_product(product, amount) order_item = OrderItem.new(product, amount) self.price_total += product.price * amount order_items << order_item endend

Rails Data Model:Order

Page 28: Contagion的Ruby/Rails投影片

"opinionated software" — sacrificing some

flexibility in favour of simplicity

Page 29: Contagion的Ruby/Rails投影片

“much greater productivity and a better developer experience”

Page 30: Contagion的Ruby/Rails投影片
Page 32: Contagion的Ruby/Rails投影片

Action grouped in Controllerclass OrdersController < ApplicationController def index list # Default to app/views/orders/index.rhtml render :action => 'list' end

def list @order_pages, @orders = paginate :orders, :per_page => 10 end

def show @order = Order.find(params[:id]) endend

Page 33: Contagion的Ruby/Rails投影片

def update @order = Order.find(params[:id]) if @order.update_attributes(params[:order]) flash[:notice] = 'Order was successfully updated.' redirect_to :action => 'show', :id => @order else render :action => 'edit' endend

Rendering and Redirection

Page 34: Contagion的Ruby/Rails投影片

Query Data

the_order = Order.find(2) book = Product.find_by_name(”Programming Ruby”)

small_order = Order.find :first, :condition => [”price_total <= ?”, 50.0]web_books = Product.find :all, :condition => [”name like ?”,”%Web%”]

print book.nameprint the_order.price_total

Page 35: Contagion的Ruby/Rails投影片

class Order < ActiveRecord::Base has_many :order_itemsend

the_order = Order.find :firstthe_oder.oder_items.each do |item| print item.nameend

Associations

* Some other associations: has_one, belongs_to, has_and_belongs_to_many...

Page 36: Contagion的Ruby/Rails投影片

Validations

Hooksclass Order < ActiveRecord::Base def before_destory order_items.destory_all endend

class Product < ActiveRecord::Base validates_presenc_of :name validtaes_numericality :priceend

Page 37: Contagion的Ruby/Rails投影片

Views - Erb<!-- app/views/product/list.rhtml --><% for product in @products -%> <p> <ul> <li> name: <%= product.name %> </li> <li> price: <%= product.price %> </li> </ul> <%= link_to 'Show', :action => 'show', :id => product %> </p><% end -%>

Page 38: Contagion的Ruby/Rails投影片

Sounds Great !!

But it is written in ....

?? Ruby ??

Page 39: Contagion的Ruby/Rails投影片

Rails makes it fast to develop, but the true power behind Rails is

RUBY

Page 40: Contagion的Ruby/Rails投影片

Blocks• Iteration

• Resource Management

• Callback

File.open(’t.txt’) do |f| #do somethingend

TkButton.new do text “EXIT” command { exit}end

5.times { |i| puts i }[1,2,3].each { |i| puts i }

Page 41: Contagion的Ruby/Rails投影片

Rakefile task :test => [:compile, :dataLoad] do # run the tests end

Markaby html do body do h1 ‘Hello World’ end end

New Language Construct

Page 42: Contagion的Ruby/Rails投影片

Dynamic Nature

•Open Class

•Runtime Definition

•Code Evaluation

•Hooks

Page 43: Contagion的Ruby/Rails投影片

Meta - Programming

class Order < ActiveRecord::Base has_many :order_itemsend

class Product < ActiveRecord::Base validates_presenc_of :name validtaes_numericality :priceend

Page 44: Contagion的Ruby/Rails投影片

Syntax Matters

•Succinct

•Easy to Read

•Principle of Least Surprise

Page 45: Contagion的Ruby/Rails投影片

Ruby is designed to make programmers

Happy

Page 46: Contagion的Ruby/Rails投影片

Where Rails Sucks

• Internationalization

•Speed

•Template System

•Stability

•Legacy System

Page 47: Contagion的Ruby/Rails投影片

LackResources

In Taiwan

Page 49: Contagion的Ruby/Rails投影片

Q&A