72
遇見 Ruby on Rails [email protected] Ruby Taiwan 2010/5@台大資工

遇見 Ruby on Rails

  • View
    9.213

  • Download
    0

Embed Size (px)

DESCRIPTION

 

Citation preview

Page 2: 遇見 Ruby on Rails

Agenda

• Who am I

• My story about web development

• Introduction to Rails

• Rails on Rails live demo

• Why Ruby?

Page 3: 遇見 Ruby on Rails

Who am I?

• 張文鈿 (a.k.a. ihower)

• 新竹高中

• 清華大學 數學系 & 資工系(輔)

• Ruby on Rails 程式設計師(since 2006)

Page 4: 遇見 Ruby on Rails

http://ihower.tw/blog/

Page 5: 遇見 Ruby on Rails

http://twitter.com/ihower

Page 6: 遇見 Ruby on Rails

http://ruby.tw

Page 7: 遇見 Ruby on Rails

Ruby Tuesday Meetings

Page 8: 遇見 Ruby on Rails

http://rubyconf.tw

Page 9: 遇見 Ruby on Rails

Where am I?台灣資訊產業台灣資訊產業

硬體產業

軟體產業

Page 10: 遇見 Ruby on Rails

SoftwareSoftwareSoftware

Desktop Mobile Web

Page 11: 遇見 Ruby on Rails

Web applicationWeb applicationWeb application

Java/.NET PHPRuby

PythonPerl

Page 12: 遇見 Ruby on Rails

My story about web development

Page 13: 遇見 Ruby on Rails

1990Tim Berners-Lee

發明 World Wide Web

Page 14: 遇見 Ruby on Rails

1999

• 那年我高一• 我用 M$ FrontPage 製作班級網頁(佳作!)

• 使用網路上免費提供的 CGI 留言板

Page 15: 遇見 Ruby on Rails

2003

• 到清大計中寫 Perl CGI

• 當初寫的程式現在還在運作中

Page 16: 遇見 Ruby on Rails

CGI, Perl用程式輸出 HTML

#!/usr/bin/perl

open(FILE, "count.txt");$num = <FILE>; $num++;close (FILE);

open(WRITETO, ">count.txt");print WRITETO "$num";close (WRITETO);

print <<PRINTAREA;content-type:text/html\n\n<style><!--body {background-color: black; line-height:1;margin-top: 0cm;margin-left: 0cm;margin-right: 0cm}--></style><body><center><b><font size=1 color=white>$num</font></b>PRINTAREA

Page 17: 遇見 Ruby on Rails

2002~2004

• 使用 Dreamweaver 製作梅竹工作會網頁

• 加入交大電子商務研究中心,使用 PHP4 & MySQL 製作動態網頁

Page 18: 遇見 Ruby on Rails

PHP在HTML中內嵌程式

<?php$counter = intval(file_get_contents("counter.dat")) + 1;$fp = fopen("counter.dat", "w");fwrite($fp, $counter);fclose($fp);?>

<p>Visitors: <?php echo @counter %></p>

Page 19: 遇見 Ruby on Rails

PHP & MySQL<?php

$db = mysql_connect("localhost", "root", "password"); mysql_query("SET NAMES 'utf8'"); mysql_select_db($SERVER['db']);?><html> <? $sql="select * from news where Class='1' or Class='3' order by CTDate desc limit 0,5"; $result= mysql_query($sql); while ( $arr=mysql_fetch_array($result) ) { echo <<<NEWSEND <div class="box"> <span class="box-title-1"> <b>【$arr[Title]】</b> $arr[CTDate] </span> <div class="box-content"> $arr[Text] </div> </div>NEWSEND; } ?></html>

Page 20: 遇見 Ruby on Rails

2003 (運作中)

Page 21: 遇見 Ruby on Rails

PHP sucks!程式碼越來越混亂

Page 22: 遇見 Ruby on Rails

2005

• 自己刻了一套 PHP web framework

• 目前有非常多的學校系所使用,運作中

Page 23: 遇見 Ruby on Rails

Web framework?

• MVC 更有系統的組織程式碼

• ORM 使用物件導向語法來操作資料庫

• URL 路徑、Template 機制、Session 處理、快取、Ajax、安全性機制......等常用功能

Page 24: 遇見 Ruby on Rails

MVC• 一種軟體架構的設計模式,將軟體分成三個部分:Model、Controller、View

• Model 物件包裝了資料與商業邏輯• 例如操作資料庫

• View 表示使用者介面,顯示及編輯表單• 可內嵌 Ruby 程式的 HTML

• Controller 負責將資料送進送出 Model

• 處理從外界(e.g. 瀏覽器)來的 Request,與 Model 互動後輸出 View (e.g. HTML)

Page 25: 遇見 Ruby on Rails

DB

Controller

View Model

1.

2.3.

4.

Page 26: 遇見 Ruby on Rails

為什麼使用 MVC?

• 分離商業邏輯和使用者介面,讓前端與後端開發者可以獨立作業

• 容易保持程式的不重複性(DRY: Don’t repeat yourself)

• 讓程式碼有著一致性的結構,位置清楚、容易維護。

Page 27: 遇見 Ruby on Rails

ORM

• Object-relational mapping

• 使用物件導向語法來操作關聯式資料庫@posts = Post.find(:all, :order => "id desc", :limit => 5 )@posts.each do |post| ...end

Page 28: 遇見 Ruby on Rails

為什麼使用 ORM?

• SQL 不是程式語言

• ORM 非常容易使用、撰碼有效率,不需要寫 routine 般的 SQL。

• 將商務邏輯與資料庫操作邏輯分開• 大家都討厭寫 SQL

Page 29: 遇見 Ruby on Rails

2006

• 遇見 Ruby on Rails

• 不需要自己重複造輪子了

Page 30: 遇見 Ruby on Rails

什麼是 Rails?• 開放原始碼 (採用MIT授權) 的 Web 框架,主要用於開發database-backed 的網站應用程式

• MVC (Model-View-Control )模式

• (幾乎可以)完全地 Ruby 開發環境,包括支援 Ajax、定義資料庫結構、ORM (object-relational-mapping) 機制操作資料庫等等。

• 2004 年由 David Heinemeier Hanson(DHH) 從 37signals 產品中獨立出來。

Page 31: 遇見 Ruby on Rails

設計原則與特色

• 不重複自己 Don’t Repeat Yourself (DRY)

• 慣例勝於設定 Convention Over Configuration

• 基於 Ruby 自然、簡潔的語法

• 更少的程式碼做更多事• 高生產力、開發迅速• 具備高度修改彈性• 寫出漂亮的程式碼、讓開發變有趣

Page 32: 遇見 Ruby on Rails

什麼樣的網站最適合使用 Ruby on Rails 開發?

• 一個資料庫形式的網站 (透過存取資料庫,動態產生

內容的網站) 或是有足夠規模到適合使用一個強大功能的框架。

• 一個特別而不適用於傳統的 CMS 系統 (若 Joomla 或 Durpal 完全符合你所需,這些系統會是更容易的選擇)

• 一個全新的應用,允許開發人員開始全新的資料庫跟軟體架構。

Page 33: 遇見 Ruby on Rails

Rails 的成長• 2005 年 DHH 從獲得年度最佳 Hacker

• 2006 年 Rails 獲得 Jolt 年度最佳產品

• 2005~2006 年 Ruby/Rails 相關書籍銷售量提昇1552%

• Ruby 程式語言在 Tiobe 的排名從 26 名提升到現在第 11 名

Page 34: 遇見 Ruby on Rails

Rails 開發有多快?Java(Spring/Hibernate) Rails

交貨時間 4個月,每週約20小時 4個晚上(每晚5小時)

程式碼行數 3293 1164

組態行數 1161 113

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

Justin Gehtland 是 Java 專家,這是他一開始採用 Rails 的實際數據

Page 35: 遇見 Ruby on Rails

Rails 有多省程式碼?

• Justin Gehtland 提出 Java :Rails = 3.5 : 1

• Proc.net 提出 PHP : Rails = 10 : 1

• JavaEye 站長估計 JAVA : Rails = 10 : 1

• thegiive(某 Rails 前輩) 實驗的結果 PHP : Rails = 8 : 1

Page 36: 遇見 Ruby on Rails

Rails clone

Page 37: 遇見 Ruby on Rails

Rails 如何運作?

C

M

V

MVCModel-View-Control

Page 38: 遇見 Ruby on Rails

Rails live demoCreating a weblog in 15 minutes

Page 39: 遇見 Ruby on Rails

User stories

• 管理員可以新增、編輯、刪除文章• 使用者可以瀏覽所有文章• 使用者可以針對文章做留言• 管理員可以刪除留言

Page 40: 遇見 Ruby on Rails

Why Ruby?

Page 41: 遇見 Ruby on Rails

什麼是 Ruby?• 開放原碼、物件導向的動態直譯式

(interpreted)程式語言

• 簡單哲學、高生產力• 精巧、自然的語法• 創造者松本行弘 a.k.a. Matz

• 靈感來自 Lisp, Perl, 和 Smalltalk

• 設計的目的是要讓程式設計師 Happy

Page 42: 遇見 Ruby on Rails

a trivial Ruby example

str = "May Ruby be with you!"5.times { puts str }

Page 43: 遇見 Ruby on Rails

the example tells us:

• 動態分型 (typing),不需要宣告型態

• 每樣東西都是物件,包括數字• 匿名函式 (code block) 隨處可見

Page 44: 遇見 Ruby on Rails

code lines matters用更少程式碼做更多事情

1. more code more bugs2. more code needs IDE3. more code needs more time to read

Page 45: 遇見 Ruby on Rails

Fred Brooks’ law

“programmer generate about same amount of code per day regardless

of the language”

Page 46: 遇見 Ruby on Rails

結論• Web framework like Rails has

• MVC

• ORM

• Ruby is

• interpreted scripting language

• dynamic typing language

• object oriented programming

Page 47: 遇見 Ruby on Rails

更多 Rails 和 Ruby 學習資源

http://ihower.tw/training

Page 48: 遇見 Ruby on Rails

One more thing...

Page 49: 遇見 Ruby on Rails

If I were a college student...如果我還是大學生

Page 50: 遇見 Ruby on Rails

1. Learn more programming language

Scripting Language: Ruby, Python, JavaScriptFunctional Language: Erlang, Scala, Haskell, Clojure

Page 51: 遇見 Ruby on Rails

2. Master Unix command Line

Page 52: 遇見 Ruby on Rails
Page 53: 遇見 Ruby on Rails
Page 54: 遇見 Ruby on Rails

3. Use Mac or Ubuntu因為 Windows 不是 Unix-like 系統

Page 55: 遇見 Ruby on Rails
Page 56: 遇見 Ruby on Rails
Page 57: 遇見 Ruby on Rails

4. Learn Git or Hg團隊開發的第一步: 版本控制系統

Page 58: 遇見 Ruby on Rails

分散式版本控制的殺手級應用

Page 59: 遇見 Ruby on Rails

Live demo(如果還有時間的話)

Page 60: 遇見 Ruby on Rails

5. Learn OOP物件導向程式設計

Page 61: 遇見 Ruby on Rails
Page 62: 遇見 Ruby on Rails

6. Be Agile軟體工程?

Page 64: 遇見 Ruby on Rails

抽象滲漏法則:

所有重大的抽象機制在某種程度上都是有

漏洞的。

http://local.joelonsoftware.com/wiki/The_Joel_on_Software_Translation_Project:抽象滲漏法則

別忽略基礎功

Page 65: 遇見 Ruby on Rails

7. Learn SQL再十年也不退流行的資料庫語言

Page 66: 遇見 Ruby on Rails
Page 67: 遇見 Ruby on Rails

8. Learn HTTP 再十年也還在用的通訊協定

Page 68: 遇見 Ruby on Rails
Page 69: 遇見 Ruby on Rails

9. 參加有趣的 Conference & Meetings

Page 70: 遇見 Ruby on Rails

• Ruby Tuesday meeting (不定期週二)

• RubyConf Taiwan (2010/4/25)

• OSDC.TW (2010/4/24)

• COSCUP (2010/8/14)

• RubyKaigi (2010/8/27 東京)

Page 71: 遇見 Ruby on Rails

更多 ihower 推薦的軟體設計書籍

http://ihower.tw/blog/books

Page 72: 遇見 Ruby on Rails

感謝聆聽,謝謝。