Contagion的Ruby/Rails投影片
-
Upload
cfc -
Category
Technology
-
view
107 -
download
2
description
Transcript of Contagion的Ruby/Rails投影片
![Page 1: Contagion的Ruby/Rails投影片](https://reader034.fdocuments.in/reader034/viewer/2022051515/54c689be4a7959163d8b4581/html5/thumbnails/1.jpg)
OSDC.TW 2006
RUBY on
![Page 2: Contagion的Ruby/Rails投影片](https://reader034.fdocuments.in/reader034/viewer/2022051515/54c689be4a7959163d8b4581/html5/thumbnails/2.jpg)
contagious
![Page 3: Contagion的Ruby/Rails投影片](https://reader034.fdocuments.in/reader034/viewer/2022051515/54c689be4a7959163d8b4581/html5/thumbnails/3.jpg)
How Hot is RoR
![Page 4: Contagion的Ruby/Rails投影片](https://reader034.fdocuments.in/reader034/viewer/2022051515/54c689be4a7959163d8b4581/html5/thumbnails/4.jpg)
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投影片](https://reader034.fdocuments.in/reader034/viewer/2022051515/54c689be4a7959163d8b4581/html5/thumbnails/5.jpg)
• 400 seats, sold out in one week
• extra 150 seats, sold out in 24 hours
![Page 6: Contagion的Ruby/Rails投影片](https://reader034.fdocuments.in/reader034/viewer/2022051515/54c689be4a7959163d8b4581/html5/thumbnails/6.jpg)
David Heinemeier
Hansson
![Page 7: Contagion的Ruby/Rails投影片](https://reader034.fdocuments.in/reader034/viewer/2022051515/54c689be4a7959163d8b4581/html5/thumbnails/7.jpg)
![Page 8: Contagion的Ruby/Rails投影片](https://reader034.fdocuments.in/reader034/viewer/2022051515/54c689be4a7959163d8b4581/html5/thumbnails/8.jpg)
![Page 9: Contagion的Ruby/Rails投影片](https://reader034.fdocuments.in/reader034/viewer/2022051515/54c689be4a7959163d8b4581/html5/thumbnails/9.jpg)
Data from BookScan:Ruby Books sales is up 1,552% over last year.
![Page 10: Contagion的Ruby/Rails投影片](https://reader034.fdocuments.in/reader034/viewer/2022051515/54c689be4a7959163d8b4581/html5/thumbnails/10.jpg)
![Page 11: Contagion的Ruby/Rails投影片](https://reader034.fdocuments.in/reader034/viewer/2022051515/54c689be4a7959163d8b4581/html5/thumbnails/11.jpg)
RoR is so HOT Everybody wants to
clone it
![Page 12: Contagion的Ruby/Rails投影片](https://reader034.fdocuments.in/reader034/viewer/2022051515/54c689be4a7959163d8b4581/html5/thumbnails/12.jpg)
![Page 13: Contagion的Ruby/Rails投影片](https://reader034.fdocuments.in/reader034/viewer/2022051515/54c689be4a7959163d8b4581/html5/thumbnails/13.jpg)
What exactly is Ruby On Rails
![Page 14: Contagion的Ruby/Rails投影片](https://reader034.fdocuments.in/reader034/viewer/2022051515/54c689be4a7959163d8b4581/html5/thumbnails/14.jpg)
Ruby on Rails is an open-source web
framework
![Page 15: Contagion的Ruby/Rails投影片](https://reader034.fdocuments.in/reader034/viewer/2022051515/54c689be4a7959163d8b4581/html5/thumbnails/15.jpg)
optimized for programmer
happiness
![Page 16: Contagion的Ruby/Rails投影片](https://reader034.fdocuments.in/reader034/viewer/2022051515/54c689be4a7959163d8b4581/html5/thumbnails/16.jpg)
What Makes Programmer Happy?
•Simplicity
• Easy to learn
•Easy to Write
•Well organized
•Easy to Maintain
![Page 17: Contagion的Ruby/Rails投影片](https://reader034.fdocuments.in/reader034/viewer/2022051515/54c689be4a7959163d8b4581/html5/thumbnails/17.jpg)
SIMPLEEasy to Learn and Write
No separation between Business Logic and Display Logic
![Page 18: Contagion的Ruby/Rails投影片](https://reader034.fdocuments.in/reader034/viewer/2022051515/54c689be4a7959163d8b4581/html5/thumbnails/18.jpg)
Corporate ApprovalWell Structured
Steep Learn CurveComplex Configuration
No fast turnaround
![Page 19: Contagion的Ruby/Rails投影片](https://reader034.fdocuments.in/reader034/viewer/2022051515/54c689be4a7959163d8b4581/html5/thumbnails/19.jpg)
Finding the middle ground with
![Page 20: Contagion的Ruby/Rails投影片](https://reader034.fdocuments.in/reader034/viewer/2022051515/54c689be4a7959163d8b4581/html5/thumbnails/20.jpg)
Full Stack of MVC
![Page 21: Contagion的Ruby/Rails投影片](https://reader034.fdocuments.in/reader034/viewer/2022051515/54c689be4a7959163d8b4581/html5/thumbnails/21.jpg)
Real World Usage
![Page 22: Contagion的Ruby/Rails投影片](https://reader034.fdocuments.in/reader034/viewer/2022051515/54c689be4a7959163d8b4581/html5/thumbnails/22.jpg)
One Language - Ruby
•Model - View - Controller
•Database Schema generation
• Javascript generation
•XML generation
•Makefile
![Page 23: Contagion的Ruby/Rails投影片](https://reader034.fdocuments.in/reader034/viewer/2022051515/54c689be4a7959163d8b4581/html5/thumbnails/23.jpg)
Convention Over
Configuration
![Page 24: Contagion的Ruby/Rails投影片](https://reader034.fdocuments.in/reader034/viewer/2022051515/54c689be4a7959163d8b4581/html5/thumbnails/24.jpg)
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投影片](https://reader034.fdocuments.in/reader034/viewer/2022051515/54c689be4a7959163d8b4581/html5/thumbnails/25.jpg)
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投影片](https://reader034.fdocuments.in/reader034/viewer/2022051515/54c689be4a7959163d8b4581/html5/thumbnails/26.jpg)
<?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投影片](https://reader034.fdocuments.in/reader034/viewer/2022051515/54c689be4a7959163d8b4581/html5/thumbnails/27.jpg)
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投影片](https://reader034.fdocuments.in/reader034/viewer/2022051515/54c689be4a7959163d8b4581/html5/thumbnails/28.jpg)
"opinionated software" — sacrificing some
flexibility in favour of simplicity
![Page 29: Contagion的Ruby/Rails投影片](https://reader034.fdocuments.in/reader034/viewer/2022051515/54c689be4a7959163d8b4581/html5/thumbnails/29.jpg)
“much greater productivity and a better developer experience”
![Page 30: Contagion的Ruby/Rails投影片](https://reader034.fdocuments.in/reader034/viewer/2022051515/54c689be4a7959163d8b4581/html5/thumbnails/30.jpg)
![Page 31: Contagion的Ruby/Rails投影片](https://reader034.fdocuments.in/reader034/viewer/2022051515/54c689be4a7959163d8b4581/html5/thumbnails/31.jpg)
Dispatch based on URLshttp://myapp/orders/show/1
Application
controlleraction
id
*customizable by Routing
![Page 32: Contagion的Ruby/Rails投影片](https://reader034.fdocuments.in/reader034/viewer/2022051515/54c689be4a7959163d8b4581/html5/thumbnails/32.jpg)
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投影片](https://reader034.fdocuments.in/reader034/viewer/2022051515/54c689be4a7959163d8b4581/html5/thumbnails/33.jpg)
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投影片](https://reader034.fdocuments.in/reader034/viewer/2022051515/54c689be4a7959163d8b4581/html5/thumbnails/34.jpg)
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投影片](https://reader034.fdocuments.in/reader034/viewer/2022051515/54c689be4a7959163d8b4581/html5/thumbnails/35.jpg)
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投影片](https://reader034.fdocuments.in/reader034/viewer/2022051515/54c689be4a7959163d8b4581/html5/thumbnails/36.jpg)
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投影片](https://reader034.fdocuments.in/reader034/viewer/2022051515/54c689be4a7959163d8b4581/html5/thumbnails/37.jpg)
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投影片](https://reader034.fdocuments.in/reader034/viewer/2022051515/54c689be4a7959163d8b4581/html5/thumbnails/38.jpg)
Sounds Great !!
But it is written in ....
?? Ruby ??
![Page 39: Contagion的Ruby/Rails投影片](https://reader034.fdocuments.in/reader034/viewer/2022051515/54c689be4a7959163d8b4581/html5/thumbnails/39.jpg)
Rails makes it fast to develop, but the true power behind Rails is
RUBY
![Page 40: Contagion的Ruby/Rails投影片](https://reader034.fdocuments.in/reader034/viewer/2022051515/54c689be4a7959163d8b4581/html5/thumbnails/40.jpg)
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投影片](https://reader034.fdocuments.in/reader034/viewer/2022051515/54c689be4a7959163d8b4581/html5/thumbnails/41.jpg)
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投影片](https://reader034.fdocuments.in/reader034/viewer/2022051515/54c689be4a7959163d8b4581/html5/thumbnails/42.jpg)
Dynamic Nature
•Open Class
•Runtime Definition
•Code Evaluation
•Hooks
![Page 43: Contagion的Ruby/Rails投影片](https://reader034.fdocuments.in/reader034/viewer/2022051515/54c689be4a7959163d8b4581/html5/thumbnails/43.jpg)
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投影片](https://reader034.fdocuments.in/reader034/viewer/2022051515/54c689be4a7959163d8b4581/html5/thumbnails/44.jpg)
Syntax Matters
•Succinct
•Easy to Read
•Principle of Least Surprise
![Page 45: Contagion的Ruby/Rails投影片](https://reader034.fdocuments.in/reader034/viewer/2022051515/54c689be4a7959163d8b4581/html5/thumbnails/45.jpg)
Ruby is designed to make programmers
Happy
![Page 46: Contagion的Ruby/Rails投影片](https://reader034.fdocuments.in/reader034/viewer/2022051515/54c689be4a7959163d8b4581/html5/thumbnails/46.jpg)
Where Rails Sucks
• Internationalization
•Speed
•Template System
•Stability
•Legacy System
![Page 47: Contagion的Ruby/Rails投影片](https://reader034.fdocuments.in/reader034/viewer/2022051515/54c689be4a7959163d8b4581/html5/thumbnails/47.jpg)
LackResources
In Taiwan
![Page 48: Contagion的Ruby/Rails投影片](https://reader034.fdocuments.in/reader034/viewer/2022051515/54c689be4a7959163d8b4581/html5/thumbnails/48.jpg)
Join Ruby.twhttp://willh.org/cfc/wiki/
![Page 49: Contagion的Ruby/Rails投影片](https://reader034.fdocuments.in/reader034/viewer/2022051515/54c689be4a7959163d8b4581/html5/thumbnails/49.jpg)
Q&A