An  INTRODUCTION   to  Ruby on Rails Nguyen Vu Hung [email_address] 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, Interactive 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 (l ambda  { |n| n ** 2 })  puts array.inspect  # => [1, 4, 9, 16]
Object oriented Similar to Java, PHP, Perl end end Point.new(@x*scalar, @y*scalar) def *(scalar)  # To perform scalar multiplication Point.new(-@x, -@y) end def -@  # Define unary minus to negate x and y Point.new(@x + other.x, @y + other.y) end def +(other)  # Define + to do vector addition @x,@y = x, y end Class Point attr_reader :x, :y  # Define accessor methods
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 chef: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 ruby-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 (make 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
 
TODO http://www.railstutorial.org/book#top http://www.railstutorial.org/chapters/a-demo-app#top http://www.railstutorial.org/chapters/static-pages#top http://www.railstutorial.org/chapters/rails-flavored-ruby#top http://www.railstutorial.org/chapters/filling-in-the-layout#top http://www.railstutorial.org/chapters/modeling-and-viewing-users-one#top http://www.railstutorial.org/chapters/modeling-and-viewing-users-two#top http://www.railstutorial.org/chapters/sign-up#top http://www.railstutorial.org/chapters/sign-in-sign-out#top http://www.railstutorial.org/chapters/updating-showing-and-deleting-users#top
TODO http://www-users.math.umd.edu/~dcarrera/ruby/0.3/chp_01/strings.html http://www-users.math.umd.edu/~dcarrera/ruby/0.3/chp_01/strings.html http://www-users.math.umd.edu/~dcarrera/ruby/0.3/chp_01/objects.html http://www-users.math.umd.edu/~dcarrera/ruby/0.3/chp_01/variables.html http://www-users.math.umd.edu/~dcarrera/ruby/0.3/chp_01/programs.html http://www-users.math.umd.edu/~dcarrera/ruby/0.3/chp_01/tips.html http://www-users.math.umd.edu/~dcarrera/ruby/0.3/chp_02/loops.html http://www-users.math.umd.edu/~dcarrera/ruby/0.3/chp_02/user_input.html http://www-users.math.umd.edu/~dcarrera/ruby/0.3/chp_02/conditionals.html http://www-users.math.umd.edu/~dcarrera/ruby/0.3/chp_02/tips.html http://www-users.math.umd.edu/~dcarrera/ruby/0.3/chp_03/arrays.html http://www-users.math.umd.edu/~dcarrera/ruby/0.3/chp_03/iterators.html http://www-users.math.umd.edu/~dcarrera/ruby/0.3/chp_03/hashes.html http://www-users.math.umd.edu/~dcarrera/ruby/0.3/chp_03/address.html http://www-users.math.umd.edu/~dcarrera/ruby/0.3/chp_03/address2.html http://www-users.math.umd.edu/~dcarrera/ruby/0.3/chp_03/sorting.html http://www-users.math.umd.edu/~dcarrera/ruby/0.3/chp_04/classes.html http://www-users.math.umd.edu/~dcarrera/ruby/0.3/chp_03/address2.html http://www-users.math.umd.edu/~dcarrera/ruby/0.3/chp_03/sorting.html http://www-users.math.umd.edu/~dcarrera/ruby/0.3/chp_04/functions.html http://www-users.math.umd.edu/~dcarrera/ruby/0.3/chp_04/classes.html http://www-users.math.umd.edu/~dcarrera/ruby/0.3/chp_04/classes2.html http://www-users.math.umd.edu/~dcarrera/ruby/0.3/chp_04/addressbook.html http://www-users.math.umd.edu/~dcarrera/ruby/0.3/chp_04/features.html
References http://en.wikipedia.org/wiki/Rapid_application_development http://en.wikipedia.org/wiki/Agile_software_development http://en.wikipedia.org/wiki/Yukihiro_Matsumoto http://en.wikipedia.org/wiki/Ruby_%28programming_language%29#Examples http://en.wikipedia.org/wiki/Ruby_%28programming_language%29 http://en.wikipedia.org/wiki/Ruby_on_Rails http://www.google.com/webhp?hl=en#hl=en&source=hp&q=ruby+functional+programming&aq=f&aqi=&aql=&oq=&gs_rfai=&fp=a86c207b1c79523e http://stackoverflow.com/questions/159797/is-ruby-a-functional-language http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/330387 http://stackoverflow.com/questions/546968/when-you-say-ruby-is-reflective-does-this-mainly-refer-to-duck-typing http://www.tutorialspoint.com/ruby/ruby_object_oriented.htm http://www.rubyist.net/~slagell/ruby/oothinking.html http://www.techotopia.com/index.php/Ruby_Object_Oriented_Programming http://d.hatena.ne.jp/shunsuk/20090101/1230816826 http://www.slideshare.net/peter_marklund/ruby-on-rails-101-presentation-slides-for-a-five-day-introductory-course http://picasaweb.google.com/Dikiwinky/Ruby#5116531304417868130 http://articles.slicehost.com/2009/4/7/centos-ruby-on-rails http://www.railstutorial.org/book

An Introduction to Ruby on Rails 20100506

  • 1.
    An INTRODUCTION to Ruby on Rails Nguyen Vu Hung [email_address] 2010/05/05
  • 2.
    Agenda Ruby –The language. Ruby on Rails
  • 3.
    Ruby – Thelanguage 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, Interactive Ruby Shell)
  • 4.
    まつもとゆきひろ Yukihiro Matsumoto松本行弘 Born 1965 Computer scientist Programmer Compiler
  • 5.
    Ruby influenced byPerl 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
  • 6.
    Functional language Emphasizesthe 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 (l ambda { |n| n ** 2 }) puts array.inspect # => [1, 4, 9, 16]
  • 7.
    Object oriented Similarto Java, PHP, Perl end end Point.new(@x*scalar, @y*scalar) def *(scalar) # To perform scalar multiplication Point.new(-@x, -@y) end def -@ # Define unary minus to negate x and y Point.new(@x + other.x, @y + other.y) end def +(other) # Define + to do vector addition @x,@y = x, y end Class Point attr_reader :x, :y # Define accessor methods
  • 8.
  • 9.
    Reflective Language TBDTBD: Java Reflection.
  • 10.
    Ruby on RailsOpen 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
  • 11.
  • 12.
    Rails architecture: 3-tier,N-tier? http://picasaweb.google.com/Dikiwinky/Ruby#5116531304417868130
  • 13.
    Rails architecture: MVCQ: Where is M, V and C?
  • 14.
    Ruby Agile DevelopmentIterative development. Self organized. Cross-functional team. Leadership philosophy: No real leader. Frequent inspection and adaptation. Allow high-quality. Rapid delivery.
  • 15.
    Ruby ScaffoldingGenerate source as needed-> Create a database (cookbook) Configure /config/database.yml Generate source code: ruby script/generate scaffold Recipe title:string chef:string instructions:text
  • 16.
    exists app/models/ existsapp/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
  • 17.
    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
  • 18.
    Ruby, Rails InstallationCentOS 5: yum install -y ruby yum install -y ruby-devel ruby-docs ruby-ri ruby-irb ruby-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
  • 19.
    Gems installed onServer 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
  • 20.
    Rake A softwarebuilding tool (automatically). Written in Ruby. Configuration file: Rakefiles, ruby syntax. Common task can be done by ruby blocks (make cannot). Rake GNU make
  • 21.
    Test-Driven Development Testfirst Write test-cases first. Automatic test cases generation. Short development circle. Regression test. Automated unit test. “Test” folder generated by Rake.
  • 22.
    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>
  • 23.
    Redmine Web basedProject 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.
  • 24.
    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
  • 25.
  • 26.
    TODO http://www.railstutorial.org/book#top http://www.railstutorial.org/chapters/a-demo-app#tophttp://www.railstutorial.org/chapters/static-pages#top http://www.railstutorial.org/chapters/rails-flavored-ruby#top http://www.railstutorial.org/chapters/filling-in-the-layout#top http://www.railstutorial.org/chapters/modeling-and-viewing-users-one#top http://www.railstutorial.org/chapters/modeling-and-viewing-users-two#top http://www.railstutorial.org/chapters/sign-up#top http://www.railstutorial.org/chapters/sign-in-sign-out#top http://www.railstutorial.org/chapters/updating-showing-and-deleting-users#top
  • 27.
    TODO http://www-users.math.umd.edu/~dcarrera/ruby/0.3/chp_01/strings.html http://www-users.math.umd.edu/~dcarrera/ruby/0.3/chp_01/strings.htmlhttp://www-users.math.umd.edu/~dcarrera/ruby/0.3/chp_01/objects.html http://www-users.math.umd.edu/~dcarrera/ruby/0.3/chp_01/variables.html http://www-users.math.umd.edu/~dcarrera/ruby/0.3/chp_01/programs.html http://www-users.math.umd.edu/~dcarrera/ruby/0.3/chp_01/tips.html http://www-users.math.umd.edu/~dcarrera/ruby/0.3/chp_02/loops.html http://www-users.math.umd.edu/~dcarrera/ruby/0.3/chp_02/user_input.html http://www-users.math.umd.edu/~dcarrera/ruby/0.3/chp_02/conditionals.html http://www-users.math.umd.edu/~dcarrera/ruby/0.3/chp_02/tips.html http://www-users.math.umd.edu/~dcarrera/ruby/0.3/chp_03/arrays.html http://www-users.math.umd.edu/~dcarrera/ruby/0.3/chp_03/iterators.html http://www-users.math.umd.edu/~dcarrera/ruby/0.3/chp_03/hashes.html http://www-users.math.umd.edu/~dcarrera/ruby/0.3/chp_03/address.html http://www-users.math.umd.edu/~dcarrera/ruby/0.3/chp_03/address2.html http://www-users.math.umd.edu/~dcarrera/ruby/0.3/chp_03/sorting.html http://www-users.math.umd.edu/~dcarrera/ruby/0.3/chp_04/classes.html http://www-users.math.umd.edu/~dcarrera/ruby/0.3/chp_03/address2.html http://www-users.math.umd.edu/~dcarrera/ruby/0.3/chp_03/sorting.html http://www-users.math.umd.edu/~dcarrera/ruby/0.3/chp_04/functions.html http://www-users.math.umd.edu/~dcarrera/ruby/0.3/chp_04/classes.html http://www-users.math.umd.edu/~dcarrera/ruby/0.3/chp_04/classes2.html http://www-users.math.umd.edu/~dcarrera/ruby/0.3/chp_04/addressbook.html http://www-users.math.umd.edu/~dcarrera/ruby/0.3/chp_04/features.html
  • 28.
    References http://en.wikipedia.org/wiki/Rapid_application_development http://en.wikipedia.org/wiki/Agile_software_developmenthttp://en.wikipedia.org/wiki/Yukihiro_Matsumoto http://en.wikipedia.org/wiki/Ruby_%28programming_language%29#Examples http://en.wikipedia.org/wiki/Ruby_%28programming_language%29 http://en.wikipedia.org/wiki/Ruby_on_Rails http://www.google.com/webhp?hl=en#hl=en&source=hp&q=ruby+functional+programming&aq=f&aqi=&aql=&oq=&gs_rfai=&fp=a86c207b1c79523e http://stackoverflow.com/questions/159797/is-ruby-a-functional-language http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/330387 http://stackoverflow.com/questions/546968/when-you-say-ruby-is-reflective-does-this-mainly-refer-to-duck-typing http://www.tutorialspoint.com/ruby/ruby_object_oriented.htm http://www.rubyist.net/~slagell/ruby/oothinking.html http://www.techotopia.com/index.php/Ruby_Object_Oriented_Programming http://d.hatena.ne.jp/shunsuk/20090101/1230816826 http://www.slideshare.net/peter_marklund/ruby-on-rails-101-presentation-slides-for-a-five-day-introductory-course http://picasaweb.google.com/Dikiwinky/Ruby#5116531304417868130 http://articles.slicehost.com/2009/4/7/centos-ruby-on-rails http://www.railstutorial.org/book