An introduction-to-ruby-on-rails

Preview:

Citation preview

An INTRODUCTION

to Ruby on Rails

Nguyen Vu Hung

vuhung@vinicorp.com.vn

2010/05/05

Agenda

• Ruby – The language.

• 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)

まつもとゆきひろ

• Yukihiro Matsumoto

• 松本行弘• Born 1965

• Computer scientist

• Programmer– Compiler

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

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]

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

Imperative Language

• TBD

Reflective Language

• TBD

• TBD: Java Reflection.

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

Rails architecture: MVC

Rails architecture: 3-tier, N-tier?

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

Rails architecture: MVC

Q: Where is M, V and C?

Ruby Agile Development

• Iterative development.

• Self organized.

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

• Frequent inspection and adaptation.

• Allow high-quality.

• Rapid delivery.

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

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

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

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

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

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

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.

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>

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.

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