遇見 ruby on rails

Post on 12-Sep-2014

9.213 Views

Category:

Documents

0 Downloads

Preview:

Click to see full reader

DESCRIPTION

 

TRANSCRIPT

遇見 Ruby on Railsihower@gmail.com

Ruby Taiwan2010/5@台大資工

Agenda

• Who am I

• My story about web development

• Introduction to Rails

• Rails on Rails live demo

• Why Ruby?

Who am I?

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

• 新竹高中

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

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

http://ihower.tw/blog/

http://twitter.com/ihower

http://ruby.tw

Ruby Tuesday Meetings

http://rubyconf.tw

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

硬體產業

軟體產業

SoftwareSoftwareSoftware

Desktop Mobile Web

Web applicationWeb applicationWeb application

Java/.NET PHPRuby

PythonPerl

My story about web development

1990Tim Berners-Lee

發明 World Wide Web

1999

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

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

2003

• 到清大計中寫 Perl CGI

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

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

2002~2004

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

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

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>

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>

2003 (運作中)

PHP sucks!程式碼越來越混亂

2005

• 自己刻了一套 PHP web framework

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

Web framework?

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

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

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

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

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

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

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

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

DB

Controller

View Model

1.

2.3.

4.

為什麼使用 MVC?

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

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

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

ORM

• Object-relational mapping

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

為什麼使用 ORM?

• SQL 不是程式語言

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

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

2006

• 遇見 Ruby on Rails

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

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

• MVC (Model-View-Control )模式

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

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

設計原則與特色

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

• 慣例勝於設定 Convention Over Configuration

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

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

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

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

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

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

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

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

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

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

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

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

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

程式碼行數 3293 1164

組態行數 1161 113

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

Justin Gehtland 是 Java 專家,這是他一開始採用 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

Rails clone

Rails 如何運作?

C

M

V

MVCModel-View-Control

Rails live demoCreating a weblog in 15 minutes

User stories

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

Why Ruby?

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

(interpreted)程式語言

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

• 靈感來自 Lisp, Perl, 和 Smalltalk

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

a trivial Ruby example

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

the example tells us:

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

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

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

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

Fred Brooks’ law

“programmer generate about same amount of code per day regardless

of the language”

結論• Web framework like Rails has

• MVC

• ORM

• Ruby is

• interpreted scripting language

• dynamic typing language

• object oriented programming

更多 Rails 和 Ruby 學習資源

http://ihower.tw/training

One more thing...

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

1. Learn more programming language

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

2. Master Unix command Line

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

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

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

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

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

6. Be Agile軟體工程?

抽象滲漏法則:

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

漏洞的。

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

別忽略基礎功

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

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

9. 參加有趣的 Conference & Meetings

• Ruby Tuesday meeting (不定期週二)

• RubyConf Taiwan (2010/4/25)

• OSDC.TW (2010/4/24)

• COSCUP (2010/8/14)

• RubyKaigi (2010/8/27 東京)

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

http://ihower.tw/blog/books

感謝聆聽,謝謝。

top related