25
An INTRODUCTION to Ruby on Rails Nguyen Vu Hung [email protected] 2010/05/05

An introduction-to-ruby-on-rails

Embed Size (px)

Citation preview

Page 1: An introduction-to-ruby-on-rails

An INTRODUCTION

to Ruby on Rails

Nguyen Vu Hung

[email protected]

2010/05/05

Page 2: An introduction-to-ruby-on-rails

Agenda

• Ruby – The language.

• Ruby on Rails

Page 3: An introduction-to-ruby-on-rails

Ruby – The language

• Made in Japan• Creator: まつもとゆきひろ• Influenced by Perl, Smalltalk, Eiffel and Lisp.• Multi-paradigm programming language:

– Functional, Object oriented, Imperative and Reflective.

• Dynamic and automatic memory management. • Written in C• Single-pass interpreted language (CLI, Interactiv

e Ruby Shell)

Page 4: An introduction-to-ruby-on-rails

まつもとゆきひろ

• Yukihiro Matsumoto

• 松本行弘• Born 1965

• Computer scientist

• Programmer– Compiler

Page 5: An introduction-to-ruby-on-rails

Ruby influenced by

• Perl– CLI– Scripting language– Simple

• Smalltalk– Object-oriented– Dynamically typed– Reflective

• Can observe and modify its own structure and behavior

• Eiffel• Lisp

– Originally specified in 1958– Fully parenthesized syntax

Page 6: An introduction-to-ruby-on-rails

Functional language

• Emphasizes the application of functions.

• Everything is a function.class Array

def iterate(code) self.each_with_index do |n, i|

self[i] = code.call(n) end

end end

array = [1, 2, 3, 4] array.iterate(lambda { |n| n ** 2 })

puts array.inspect # => [1, 4, 9, 16]

Page 7: An introduction-to-ruby-on-rails

Object oriented

• Similar to Java, PHP, Perl

Class Point

attr_reader :x, :y # Define accessor methods

@x,@y = x, y end

def +(other) # Define + to do vector addition

Point.new(@x + other.x, @y + other.y) end

def -@ # Define unary minus to negate x and y

Point.new(-@x, -@y) end

def *(scalar) # To perform scalar multiplication

Point.new(@x*scalar, @y*scalar)

end

end

Page 8: An introduction-to-ruby-on-rails

Imperative Language

• TBD

Page 9: An introduction-to-ruby-on-rails

Reflective Language

• TBD

• TBD: Java Reflection.

Page 10: An introduction-to-ruby-on-rails

Ruby on Rails

• Open source web application framework– MIT license.

• Used with Agile development methodology• Rail architecture

– A MVC model• Scaffolding: Automatically creates a skeleton of a basic

website.• WEBrick. Mongrel: Web servers.• Rake: A build system.• Test-Driven• ActiveRecord

– An object-relational mapping system for database access

Page 11: An introduction-to-ruby-on-rails

Rails architecture: MVC

Page 12: An introduction-to-ruby-on-rails

Rails architecture: 3-tier, N-tier?

•http://picasaweb.google.com/Dikiwinky/Ruby#5116531304417868130

Page 13: An introduction-to-ruby-on-rails

Rails architecture: MVC

Q: Where is M, V and C?

Page 14: An introduction-to-ruby-on-rails

Ruby Agile Development

• Iterative development.

• Self organized.

• Cross-functional team.– Leadership philosophy: No real leader.

• Frequent inspection and adaptation.

• Allow high-quality.

• Rapid delivery.

Page 15: An introduction-to-ruby-on-rails

Ruby Scaffolding• Generate source as needed→

• Create a database (cookbook)

• Configure /config/database.yml

• Generate source code:– ruby script/generate scaffold Recipe title:string ch

ef:string instructions:text

Page 16: An introduction-to-ruby-on-rails

exists app/models/ exists app/controllers/ exists app/helpers/ create app/views/recipes exists app/views/layouts/ exists test/functional/ exists test/unit/ exists public/stylesheets/ create app/views/recipes/index.html.erb create app/views/recipes/show.html.erb create app/views/recipes/new.html.erb create app/views/recipes/edit.html.erb create app/views/layouts/recipes.html.erb create public/stylesheets/scaffold.css create app/controllers/recipes_controller.rb create test/functional/recipes_controller_test.rb create app/helpers/recipes_helper.rb route map.resources :recipes

dependency model exists app/models/ exists test/unit/ exists test/fixtures/ create app/models/recipe.rb create test/unit/recipe_test.rb create test/fixtures/recipes.yml create db/migrate create db/migrate/20080614192220_create_recipes.rb

Page 17: An introduction-to-ruby-on-rails

exists app/models/ exists app/controllers/ exists app/helpers/ create app/views/recipes exists app/views/layouts/ exists test/functional/ exists test/unit/ exists public/stylesheets/ create app/views/recipes/index.html.erb create app/views/recipes/show.html.erb create app/views/recipes/new.html.erb create app/views/recipes/edit.html.erb create app/views/layouts/recipes.html.erb create public/stylesheets/scaffold.css create app/controllers/recipes_controller.rb create test/functional/recipes_controller_test.rb create app/helpers/recipes_helper.rb route map.resources :recipes

dependency model exists app/models/ exists test/unit/ exists test/fixtures/ create app/models/recipe.rb create test/unit/recipe_test.rb create test/fixtures/recipes.yml create db/migrate create db/migrate/20080614192220_create_recipes.rb

Page 18: An introduction-to-ruby-on-rails

Ruby, Rails Installation

• CentOS 5:– yum install -y ruby– yum install -y ruby-devel ruby-docs ruby-ri ruby-irb rub

y-rdoc– tar xzvf rubygems-1.3.1.tgz– cd rubygems– sudo ruby setup.rb – sudo gem update – sudo gem install rails

• Windows– http://rubyonrails.org/download

Page 19: An introduction-to-ruby-on-rails

Gems installed on Server 123• [vuhung@vinicorp ~]$ sudo gem list

• *** LOCAL GEMS ***

• actionmailer (2.3.5, 2.2.2)• actionpack (2.3.5, 2.2.2)• activerecord (2.3.5, 2.2.2)• activeresource (2.3.5, 2.2.2)• activesupport (2.3.5, 2.2.2)• eventmachine (0.12.10)• fastthread (1.0.7)• htmlentities (4.2.0)• json (1.2.0)• juggernaut (0.5.8)• passenger (2.2.9)• rack (1.0.1)• rails (2.3.5, 2.2.2)• rake (0.8.7

Page 20: An introduction-to-ruby-on-rails

Rake• A software building tool (automatically).

• Written in Ruby.

• Configuration file: Rakefiles, ruby syntax.– Common task can be done by ruby blocks (m

ake cannot).

Rake GNU make

Page 21: An introduction-to-ruby-on-rails

Test-Driven Development

• Test first– Write test-cases first.– Automatic test cases generation.

• Short development circle.

• Regression test.

• Automated unit test.

• “Test” folder generated by Rake.

Page 22: An introduction-to-ruby-on-rails

A short tutorial• [vuhung@vinicorp ~]$ irb• irb(main):001:0> puts "Hello Worlds"• Hello Worlds• => nil• irb(main):002:0> 3+2• => 5• irb(main):003:0> 3*2• => 6• irb(main):004:0> 3**2• => 9• irb(main):005:0> Math.sqrt(9)• => 3.0• irb(main):006:0> a = 3**2• => 9• irb(main):007:0> b = 4**2• => 16• irb(main):008:0> Math.sqrt(a+b)• => 5.0• irb(main):009:0>

Page 23: An introduction-to-ruby-on-rails

Redmine

• Web based Project Management Application.• Written in Ruby on Rails• GPL v2 licensed (free software).• Rake as build system. • Gems• WEBrick• Plenty of plugins available.• Stable since 2009/11 to date.• Supports various DB back-end after Rails.

Page 24: An introduction-to-ruby-on-rails

Redmine Folder Structure (2)• ls -1 /var/www/html/redmine/redmine-0.8.7• app• config• db• doc• extra• files• lang• lib• log• public• Rakefile• script• start_redmine.sh• test• tmp• vendor

Page 25: An introduction-to-ruby-on-rails