56
Ruby on Rails 探索 kewang

Ruby on rails探索

Embed Size (px)

Citation preview

Page 1: Ruby on rails探索

Ruby on Rails探索

kewang

Page 2: Ruby on rails探索

2

Agenda

● What is Ruby?● What is Rails?● Live DEMO● FAQ● Q&A

Page 3: Ruby on rails探索

3

What is Ruby?鑽石恆久遠,一顆就破產

Page 4: Ruby on rails探索

4

What is Ruby?

● 開放原碼、物件導向的動態直譯式程式語言● 簡單哲學、高生產力● 精巧、自然的語法● 靈感來自 Lisp, Perl, Smalltalk● 設計的目的是要讓程式設計師 Happy

Page 5: Ruby on rails探索

5

Matz說

● 一般程式語言的開發目的● 可寫出能夠高速運作的程式● 可在短期間學會寫程式● 寫一次到處都能跑● 小朋友也能輕易寫出程式

Page 6: Ruby on rails探索

6

但主張「讓寫程式更快樂」的語言卻不太常見

Page 7: Ruby on rails探索

7

Matz

Ruby is simple in appearance, but is very complex inside, just like our human body.

Page 8: Ruby on rails探索

8

Programming Language Type

● Dynamic Strong Language● Ruby● Perl● Python

● PHP is Dynamic, but is not Strong.● Java is Strong, but is not Dynamic.

Page 9: Ruby on rails探索

9

EVERYTHING IS OBJECT

Page 10: Ruby on rails探索

10

irb – Interactive Ruby

Page 11: Ruby on rails探索

11

TIOBE Ranking

Page 12: Ruby on rails探索

12

Features

● Iterator● Code block (closure)● ! and ?● Meta-programming

Page 13: Ruby on rails探索

13

Iterator

● 重複做多次類似的事情● upto, downto, step, times, collect, map, each_*,

*_each, sort...many methods

Page 14: Ruby on rails探索

14

Code block (closure)

Page 15: Ruby on rails探索

15

Code block (closure)

Page 16: Ruby on rails探索

16

Code block (closure)

Page 17: Ruby on rails探索

17

Code block (closure)

Page 18: Ruby on rails探索

18

! and ?

● ! means side-effect● ? means return Boolean

Page 19: Ruby on rails探索

19

! and ?

Page 20: Ruby on rails探索

20

Meta-programming

Page 21: Ruby on rails探索

21

RubyGems

● Ruby 的套件管理工具● 所有的 Ruby 套件都可以用 gem 安裝

● Rails● RMagick● Heroku● ...etc.

● http://rubygems.org

Page 22: Ruby on rails探索

22

Sinatra

● It's a VERY lightweight web development package● gem install sinatra● http://www.sinatrarb.com

Page 23: Ruby on rails探索

23

Hello Sinatra

● ruby myapp.rb, and open http://localhost:4567

Page 24: Ruby on rails探索

24

Hello Parameter

Page 25: Ruby on rails探索

25

Sinatra - XDite如是說

使用 Sinatra 的目的並不是用來開發那些巨型的 Web application ,而是搭造那些小型的應用程式或者 API 介面。這樣做有些什麼好處呢?

如果你只是想寫一支小型程式,或者開發API ,並不需要使用 Rails 這麼複雜 ( 或肥 )的 Framework 做這些簡單的事。使用 Sinatra既簡單, Respond 也迅速。

Page 26: Ruby on rails探索

26

What is Rails?火車環島正夯

Page 27: Ruby on rails探索

27

What is Rails?

● It's a Web Framework

Page 28: Ruby on rails探索

28

What is Rails?

Page 29: Ruby on rails探索

29

What is Rails?

● DHH● 從 37signals 公司的專案

管理工具 Basecamp 裡面分離出 Ruby on Rails

● 2005 年獲得年度最佳 Hacker

Page 30: Ruby on rails探索

30

What is Rails?

● 正式名稱 Ruby on Rails

● 使用 MVC(Model-View-Control)● 內建 unit / integration test● 支援 Ajax, RESTfFul, ORM

● 支援最新技術 HTML5, jQuery, SASS, HAML, Coffee Script

Page 31: Ruby on rails探索

31

Design Principles

DRYDon't Repeat Yourself

Page 32: Ruby on rails探索

32

Design Principles

CoCConvention over Configuration

Page 33: Ruby on rails探索

33

Rails is so FAST!!!

JSP Rails

交貨時間4 個月,每週約 20小時

4 個晚上,每晚 5 小時

程式碼行數 3293 1164

設定檔行數 1161 113

類別數 / 方法數 62/549 55/126

Page 34: Ruby on rails探索

34

What is MVC

Page 35: Ruby on rails探索

35

What is MVC

● Model 包裝了資料與商業邏輯,例如操作資料庫● View 表示使用者介面,顯示及編輯表單● Controller 將資料送進送出 Model ,處理從外界

來的 HTTP Request ,與 Model 互動後輸出至View

Page 36: Ruby on rails探索

36

DB schema

Page 37: Ruby on rails探索

37

ActiveRecord (M)

Page 38: Ruby on rails探索

38

Rails console

Page 39: Ruby on rails探索

39

Rails console

Page 40: Ruby on rails探索

40

ActionController (C)

Page 41: Ruby on rails探索

41

ActionView (V)

Page 42: Ruby on rails探索

42

What is URL routing?

Page 43: Ruby on rails探索

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

Page 44: Ruby on rails探索

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

Page 45: Ruby on rails探索

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

Page 46: Ruby on rails探索

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

Page 47: Ruby on rails探索

47

One action, multiple formats

Page 48: Ruby on rails探索

48

One action, multiple formats

Page 49: Ruby on rails探索

49

Live DEMO...Never Live DEMO

Page 50: Ruby on rails探索

50

FAQFxxk you!?

Page 51: Ruby on rails探索

51

Why Ruby?

● Domain-specific language● Full object-oriented programming● High usability● High readability, maintenance

Page 52: Ruby on rails探索

52

Why Rails?

● The most successful Web Framework● Imitation is the greatest compliment

● CakePHP● Grails● TurboGears● catalyst

Page 53: Ruby on rails探索

53

Websites

Page 54: Ruby on rails探索

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

Page 55: Ruby on rails探索

55

Performance is slow?

● 如果一個框架可以讓你僅變快 20% ,或許你應該繼續使用比較保險的語言,像是 Java 。但是如果你可以變快 300% 甚至更高,那麼其他的差異都變的不重要了 from Beyond Java

Page 56: Ruby on rails探索

56

Q&AQuick Asleep