Upload
mu-chun-wang
View
611
Download
1
Embed Size (px)
Citation preview
Ruby on Rails探索
kewang
2
Agenda
● What is Ruby?● What is Rails?● Live DEMO● FAQ● Q&A
3
What is Ruby?鑽石恆久遠,一顆就破產
4
What is Ruby?
● 開放原碼、物件導向的動態直譯式程式語言● 簡單哲學、高生產力● 精巧、自然的語法● 靈感來自 Lisp, Perl, Smalltalk● 設計的目的是要讓程式設計師 Happy
5
Matz說
● 一般程式語言的開發目的● 可寫出能夠高速運作的程式● 可在短期間學會寫程式● 寫一次到處都能跑● 小朋友也能輕易寫出程式
6
但主張「讓寫程式更快樂」的語言卻不太常見
7
Matz
Ruby is simple in appearance, but is very complex inside, just like our human body.
8
Programming Language Type
● Dynamic Strong Language● Ruby● Perl● Python
● PHP is Dynamic, but is not Strong.● Java is Strong, but is not Dynamic.
9
EVERYTHING IS OBJECT
10
irb – Interactive Ruby
11
TIOBE Ranking
12
Features
● Iterator● Code block (closure)● ! and ?● Meta-programming
13
Iterator
● 重複做多次類似的事情● upto, downto, step, times, collect, map, each_*,
*_each, sort...many methods
14
Code block (closure)
15
Code block (closure)
16
Code block (closure)
17
Code block (closure)
18
! and ?
● ! means side-effect● ? means return Boolean
19
! and ?
20
Meta-programming
21
RubyGems
● Ruby 的套件管理工具● 所有的 Ruby 套件都可以用 gem 安裝
● Rails● RMagick● Heroku● ...etc.
● http://rubygems.org
22
Sinatra
● It's a VERY lightweight web development package● gem install sinatra● http://www.sinatrarb.com
23
Hello Sinatra
● ruby myapp.rb, and open http://localhost:4567
24
Hello Parameter
25
Sinatra - XDite如是說
使用 Sinatra 的目的並不是用來開發那些巨型的 Web application ,而是搭造那些小型的應用程式或者 API 介面。這樣做有些什麼好處呢?
如果你只是想寫一支小型程式,或者開發API ,並不需要使用 Rails 這麼複雜 ( 或肥 )的 Framework 做這些簡單的事。使用 Sinatra既簡單, Respond 也迅速。
26
What is Rails?火車環島正夯
27
What is Rails?
● It's a Web Framework
28
What is Rails?
29
What is Rails?
● DHH● 從 37signals 公司的專案
管理工具 Basecamp 裡面分離出 Ruby on Rails
● 2005 年獲得年度最佳 Hacker
30
What is Rails?
● 正式名稱 Ruby on Rails
● 使用 MVC(Model-View-Control)● 內建 unit / integration test● 支援 Ajax, RESTfFul, ORM
● 支援最新技術 HTML5, jQuery, SASS, HAML, Coffee Script
31
Design Principles
DRYDon't Repeat Yourself
32
Design Principles
CoCConvention over Configuration
33
Rails is so FAST!!!
JSP Rails
交貨時間4 個月,每週約 20小時
4 個晚上,每晚 5 小時
程式碼行數 3293 1164
設定檔行數 1161 113
類別數 / 方法數 62/549 55/126
34
What is MVC
35
What is MVC
● Model 包裝了資料與商業邏輯,例如操作資料庫● View 表示使用者介面,顯示及編輯表單● Controller 將資料送進送出 Model ,處理從外界
來的 HTTP Request ,與 Model 互動後輸出至View
36
DB schema
37
ActiveRecord (M)
38
Rails console
39
Rails console
40
ActionController (C)
41
ActionView (V)
42
What is URL routing?
43
a bloated controllerclass EventController < ApplicationController
index /events/index
show /events/show/1
new /events/new
create /events/create
show_comment /events/3/show_comment/6
mark_spamcomment /events/7/mark_spamcomment/5
add_favorite /events/2/add_favorite
invite /events/1/invite
deny_user /events/1/deny_user/13
allow_user /events/2/allow_user/27
44
What is RESTful?
before after ActionController
/events/create POST /events events#create
/events/show/1 GET /events/1 events#show
/events/update/1 PUT /events/1 events#update
/events/destroy/1 DELETE /events/1 events#destroy
45
Let's modify bloated controllerclass EventController < ApplicationController
index GET /events events#index
show GET /events/1 events#show
new GET /events/new events#new
create POST /events events#create
show_comment GET /events/3/comments/6 event_comments#show
mark_spamcomment PUT /events/7/comments/5/spam event_comments#spam
add_favorite POST /events/2/favorite events#favorite
invite POST /events/1/invite events#invite
deny_user PUT /events/1/users/13/deny event_users#deny
allow_user PUT /events/2/users/27/allow event_users#allow
46
RESTful routing
HTTP Verb Path actionGET /photos index
GET /photos/new new
POST /photos create
GET /photos/:id show
GET /photos/:id/edit edit
PUT /photos/:id update
DELETE /photos/:id destroy
47
One action, multiple formats
48
One action, multiple formats
49
Live DEMO...Never Live DEMO
50
FAQFxxk you!?
51
Why Ruby?
● Domain-specific language● Full object-oriented programming● High usability● High readability, maintenance
52
Why Rails?
● The most successful Web Framework● Imitation is the greatest compliment
● CakePHP● Grails● TurboGears● catalyst
53
Websites
54
Java 1.6
C++ 4.2.3
Ruby 1.9.0
Python 2.5.1
PHP 5.2.3
0 100 200 300 400 500 600 700
Performance Comparison
Lines of CodeTime per iteration (microseconds)
愈小愈好
Lang
uage
55
Performance is slow?
● 如果一個框架可以讓你僅變快 20% ,或許你應該繼續使用比較保險的語言,像是 Java 。但是如果你可以變快 300% 甚至更高,那麼其他的差異都變的不重要了 from Beyond Java
56
Q&AQuick Asleep