RUBY   on


  OSDC.TW 2006
contagious
How Hot is RoR
450,000
                 over 400,000
                  downloads
337,500


225,000


112,500


     0
    2004 July   200...
•   400 seats, sold out in one week

•   extra 150 seats, sold out in 24 hours
David
Heinemeier
 Hansson
Data from BookScan:
Ruby Books sales is up 1,552%
over last year.
RoR is so HOT
Everybody wants to
      clone it
What exactly is
Ruby On Rails
Ruby on Rails is an
open-source web
   framework
optimized for
programmer
 happiness
What Makes Programmer
        Happy?

• Simplicity       • Well organized
 • Easy to learn    • Easy to Maintain
 • Easy t...
SIMPLE
    Easy to Learn and Write



    No separation between
Business Logic and Display Logic
Corporate Approval
  Well Structured


  Steep Learn Curve
Complex Configuration
 No fast turnaround
Finding the middle
   ground with
Full Stack of MVC
Real World Usage
One Language - Ruby
• Model - View - Controller
• Database Schema generation
• Javascript generation
• XML generation
• Ma...
Convention
   Over
Configuration
CREATE TABLE orders (
  id INTEGER PRIMARY KEY AUTO_INCREMENT,


                                              An
  order_...
public class Order


                                                  Java
{
    private Integer id;
    private Date dat...
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping
    PUBLIC "-//Hibernate/Hibernate Mapping DTD//EN"
   ...
Rails Data Model:
               Order
class Order < ActiveRecord::Base
  has_many :order_items

  def add_product(product...
"opinionated software"
  — sacrificing some
 flexibility in favour of
       simplicity
“much greater
productivity and a better
 developer experience”
Dispatch based on URLs
 http://myapp/orders/show/1

     Application
                   controller
                       ...
Action grouped in Controller
class OrdersController < ApplicationController
  def index
    list
    # Default to app/view...
Rendering and Redirection
def update
  @order = Order.find(params[:id])
  if @order.update_attributes(params[:order])
    ...
Query Data
the_order = Order.find(2)
book = Product.find_by_name(”Programming Ruby”)

small_order = Order.find :first,
   ...
Associations
class Order < ActiveRecord::Base
  has_many :order_items
end

the_order = Order.find :first
the_oder.oder_ite...
Validations
class Product < ActiveRecord::Base
  validates_presenc_of   :name
  validtaes_numericality :price
end


      ...
Views - Erb
<!-- app/views/product/list.rhtml -->
<% for product in @products -%>
  <p>
    <ul>
       <li> name: <%= pro...
Sounds Great !!

But it is written in ....

      ?? Ruby ??
Rails makes it fast to
develop, but the true
power behind Rails is


        RUBY
Blocks
• Iteration
   5.times { |i| puts i }
   [1,2,3].each { |i| puts i }

• Resource Management
   File.open(’t.txt’) d...
New Language Construct
 Rakefile
  task :test => [:compile, :dataLoad] do
    # run the tests
  end


 Markaby
  html do
  ...
Dynamic Nature

•Open Class
• Runtime Definition
• Code Evaluation
• Hooks
Meta - Programming
class Order < ActiveRecord::Base
  has_many :order_items
end


class Product < ActiveRecord::Base
  val...
Syntax Matters

• Succinct
• Easy to Read
• Principle of Least Surprise
Ruby is designed to
make programmers
      Happy
Where Rails Sucks
•Internationalization
• Speed
• Template System
• Stability
• Legacy System
Lack
Resources
    In
 Taiwan
Join Ruby.tw
http://willh.org/cfc/wiki/
Q&A
Contagion的Ruby/Rails投影片
Contagion的Ruby/Rails投影片
Contagion的Ruby/Rails投影片
Contagion的Ruby/Rails投影片
Contagion的Ruby/Rails投影片
Upcoming SlideShare
Loading in...5
×

Contagion的Ruby/Rails投影片

2,002

Published on

有點舊了:P

Published in: Technology
0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total Views
2,002
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
118
Comments
0
Likes
1
Embeds 0
No embeds

No notes for slide

Transcript of "Contagion的Ruby/Rails投影片"

  1. 1. RUBY on OSDC.TW 2006
  2. 2. contagious
  3. 3. How Hot is RoR
  4. 4. 450,000 over 400,000 downloads 337,500 225,000 112,500 0 2004 July 2004 Dec. 2005 Oct. 2006 April
  5. 5. • 400 seats, sold out in one week • extra 150 seats, sold out in 24 hours
  6. 6. David Heinemeier Hansson
  7. 7. Data from BookScan: Ruby Books sales is up 1,552% over last year.
  8. 8. RoR is so HOT Everybody wants to clone it
  9. 9. What exactly is Ruby On Rails
  10. 10. Ruby on Rails is an open-source web framework
  11. 11. optimized for programmer happiness
  12. 12. What Makes Programmer Happy? • Simplicity • Well organized • Easy to learn • Easy to Maintain • Easy to Write
  13. 13. SIMPLE Easy to Learn and Write No separation between Business Logic and Display Logic
  14. 14. Corporate Approval Well Structured Steep Learn Curve Complex Configuration No fast turnaround
  15. 15. Finding the middle ground with
  16. 16. Full Stack of MVC
  17. 17. Real World Usage
  18. 18. One Language - Ruby • Model - View - Controller • Database Schema generation • Javascript generation • XML generation • Makefile
  19. 19. Convention Over Configuration
  20. 20. CREATE TABLE orders ( id INTEGER PRIMARY KEY AUTO_INCREMENT, An order_date TIMESTAMP NOT NULL, price_total DOUBLE NOT NULL ); example CREATE TABLE products ( id INTEGER PRIMARY KEY AUTO_INCREMENT, from a name VARCHAR(80) NOT NULL, price DOUBLE NOT NULL ); Hibernate CREATE TABLE order_items ( id INTEGER PRIMARY KEY AUTO_INCREMENT, tutorial order_id INTEGER NOT NULL, product_id INTEGER NOT NULL, amount INTEGER NOT NULL, price DOUBLE NOT NULL );
  21. 21. public class Order Java { private Integer id; private Date date; private double priceTotal; Object private Set orderItems = new HashSet(); public Order() { Model: this.date = new Date(); } public Integer getId() { Order 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); } }
  22. 22. <?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"> Java <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"/> Database <generator class="native"/> </id> <property name="date"> Mapping: <column name="order_date" sql-type="datetime" not-null="true"/> </property> Order <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>
  23. 23. Rails Data Model: Order 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 end end
  24. 24. "opinionated software" — sacrificing some flexibility in favour of simplicity
  25. 25. “much greater productivity and a better developer experience”
  26. 26. Dispatch based on URLs http://myapp/orders/show/1 Application controller action id *customizable by Routing
  27. 27. Action grouped in Controller class 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]) end end
  28. 28. Rendering and Redirection 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' end end
  29. 29. 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.name print the_order.price_total
  30. 30. Associations class Order < ActiveRecord::Base has_many :order_items end the_order = Order.find :first the_oder.oder_items.each do |item| print item.name end * Some other associations: has_one, belongs_to, has_and_belongs_to_many...
  31. 31. Validations class Product < ActiveRecord::Base validates_presenc_of :name validtaes_numericality :price end Hooks class Order < ActiveRecord::Base def before_destory order_items.destory_all end end
  32. 32. 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 -%>
  33. 33. Sounds Great !! But it is written in .... ?? Ruby ??
  34. 34. Rails makes it fast to develop, but the true power behind Rails is RUBY
  35. 35. Blocks • Iteration 5.times { |i| puts i } [1,2,3].each { |i| puts i } • Resource Management File.open(’t.txt’) do |f| #do something end • Callback TkButton.new do text “EXIT” command { exit} end
  36. 36. New Language Construct Rakefile task :test => [:compile, :dataLoad] do # run the tests end Markaby html do body do h1 ‘Hello World’ end end
  37. 37. Dynamic Nature •Open Class • Runtime Definition • Code Evaluation • Hooks
  38. 38. Meta - Programming class Order < ActiveRecord::Base has_many :order_items end class Product < ActiveRecord::Base validates_presenc_of :name validtaes_numericality :price end
  39. 39. Syntax Matters • Succinct • Easy to Read • Principle of Least Surprise
  40. 40. Ruby is designed to make programmers Happy
  41. 41. Where Rails Sucks •Internationalization • Speed • Template System • Stability • Legacy System
  42. 42. Lack Resources In Taiwan
  43. 43. Join Ruby.tw http://willh.org/cfc/wiki/
  44. 44. Q&A
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.

×