An Introduction to Ruby on Rails 20100506

1,629 views

Published on

Published in: Technology
1 Comment
4 Likes
Statistics
Notes
No Downloads
Views
Total views
1,629
On SlideShare
0
From Embeds
0
Number of Embeds
46
Actions
Shares
0
Downloads
36
Comments
1
Likes
4
Embeds 0
No embeds

No notes for slide

An Introduction to Ruby on Rails 20100506

  1. 1. An INTRODUCTION to Ruby on Rails Nguyen Vu Hung [email_address] 2010/05/05
  2. 2. Agenda <ul><li>Ruby – The language. </li></ul><ul><li>Ruby on Rails </li></ul>
  3. 3. Ruby – The language <ul><li>Made in Japan </li></ul><ul><li>Creator: まつもとゆきひろ </li></ul><ul><li>Influenced by Perl, Smalltalk, Eiffel and Lisp. </li></ul><ul><li>Multi-paradigm programming language: </li></ul><ul><ul><li>Functional, Object oriented, Imperative and Reflective. </li></ul></ul><ul><li>Dynamic and automatic memory management. </li></ul><ul><li>Written in C </li></ul><ul><li>Single-pass interpreted language (CLI, Interactive Ruby Shell) </li></ul>
  4. 4. まつもとゆきひろ <ul><li>Yukihiro Matsumoto </li></ul><ul><li>松本行弘 </li></ul><ul><li>Born 1965 </li></ul><ul><li>Computer scientist </li></ul><ul><li>Programmer </li></ul><ul><ul><li>Compiler </li></ul></ul>
  5. 5. Ruby influenced by <ul><li>Perl </li></ul><ul><ul><li>CLI </li></ul></ul><ul><ul><li>Scripting language </li></ul></ul><ul><ul><li>Simple </li></ul></ul><ul><li>Smalltalk </li></ul><ul><ul><li>Object-oriented </li></ul></ul><ul><ul><li>Dynamically typed </li></ul></ul><ul><ul><li>Reflective </li></ul></ul><ul><ul><ul><li>Can observe and modify its own structure and behavior </li></ul></ul></ul><ul><li>Eiffel </li></ul><ul><li>Lisp </li></ul><ul><ul><li>Originally specified in 1958 </li></ul></ul><ul><ul><li>Fully parenthesized syntax </li></ul></ul>
  6. 6. Functional language <ul><li>Emphasizes the application of functions. </li></ul><ul><li>Everything is a function. </li></ul>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. 7. Object oriented <ul><li>Similar to Java, PHP, Perl </li></ul>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. 8. Imperative Language <ul><li>TBD </li></ul>
  9. 9. Reflective Language <ul><li>TBD </li></ul><ul><li>TBD: Java Reflection. </li></ul>
  10. 10. Ruby on Rails <ul><li>Open source web application framework </li></ul><ul><ul><li>MIT license. </li></ul></ul><ul><li>Used with Agile development methodology </li></ul><ul><li>Rail architecture </li></ul><ul><ul><li>A MVC model </li></ul></ul><ul><li>Scaffolding : Automatically creates a skeleton of a basic website. </li></ul><ul><li>WEBrick. Mongrel: Web servers. </li></ul><ul><li>Rake: A build system. </li></ul><ul><li>Test-Driven </li></ul><ul><li>ActiveRecord </li></ul><ul><ul><li>An object-relational mapping system for database access </li></ul></ul>
  11. 11. Rails architecture: MVC
  12. 12. Rails architecture: 3-tier, N-tier? <ul><li>http://picasaweb.google.com/Dikiwinky/Ruby#5116531304417868130 </li></ul>
  13. 13. Rails architecture: MVC Q: Where is M, V and C?
  14. 14. Ruby Agile Development <ul><li>Iterative development. </li></ul><ul><li>Self organized. </li></ul><ul><li>Cross-functional team. </li></ul><ul><ul><li>Leadership philosophy: No real leader. </li></ul></ul><ul><li>Frequent inspection and adaptation. </li></ul><ul><li>Allow high-quality. </li></ul><ul><li>Rapid delivery. </li></ul>
  15. 15. Ruby Scaffolding <ul><li>Generate source as needed-> </li></ul><ul><li>Create a database (cookbook) </li></ul><ul><li>Configure /config/database.yml </li></ul><ul><li>Generate source code: </li></ul><ul><ul><li>ruby script/generate scaffold Recipe title:string chef:string instructions:text </li></ul></ul>
  16. 16. <ul><li>exists app/models/ exists app/controllers/ </li></ul><ul><li>exists app/helpers/ create app/views/recipes </li></ul><ul><li>exists app/views/layouts/ exists test/functional/ </li></ul><ul><li>exists test/unit/ exists public/stylesheets/ </li></ul><ul><li>create app/views/recipes/index.html.erb </li></ul><ul><li>create app/views/recipes/show.html.erb </li></ul><ul><li>create app/views/recipes/new.html.erb </li></ul><ul><li>create app/views/recipes/edit.html.erb </li></ul><ul><li>create app/views/layouts/recipes.html.erb </li></ul><ul><li>create public/stylesheets/scaffold.css </li></ul><ul><li>create app/controllers/recipes_controller.rb </li></ul><ul><li>create test/functional/recipes_controller_test.rb </li></ul><ul><li>create app/helpers/recipes_helper.rb </li></ul><ul><li>route map.resources :recipes </li></ul><ul><li>dependency model exists </li></ul><ul><li>app/models/ exists test/unit/ exists </li></ul><ul><li>test/fixtures/ create app/models/recipe.rb </li></ul><ul><li>create test/unit/recipe_test.rb </li></ul><ul><li>create test/fixtures/recipes.yml </li></ul><ul><li>create db/migrate </li></ul><ul><li>create db/migrate/20080614192220_create_recipes.rb </li></ul>
  17. 17. <ul><li>exists app/models/ </li></ul><ul><li>exists app/controllers/ </li></ul><ul><li>exists app/helpers/ </li></ul><ul><li>create app/views/recipes </li></ul><ul><li>exists app/views/layouts/ exists test/functional/ </li></ul><ul><li>exists test/unit/ exists public/stylesheets/ </li></ul><ul><li>create app/views/recipes/index.html.erb </li></ul><ul><li>create app/views/recipes/show.html.erb </li></ul><ul><li>create app/views/recipes/new.html.erb </li></ul><ul><li>create app/views/recipes/edit.html.erb </li></ul><ul><li>create app/views/layouts/recipes.html.erb </li></ul><ul><li>create public/stylesheets/scaffold.css </li></ul><ul><li>create app/controllers/recipes_controller.rb </li></ul><ul><li>create test/functional/recipes_controller_test.rb </li></ul><ul><li>create app/helpers/recipes_helper.rb </li></ul><ul><li>route map.resources :recipes </li></ul><ul><li>dependency model exists </li></ul><ul><li>app/models/ exists test/unit/ exists </li></ul><ul><li>test/fixtures/ create app/models/recipe.rb </li></ul><ul><li>create test/unit/recipe_test.rb </li></ul><ul><li>create test/fixtures/recipes.yml </li></ul><ul><li>create db/migrate </li></ul><ul><li>create db/migrate/20080614192220_create_recipes.rb </li></ul>
  18. 18. Ruby, Rails Installation <ul><li>CentOS 5: </li></ul><ul><ul><li>yum install -y ruby </li></ul></ul><ul><ul><li>yum install -y ruby-devel ruby-docs ruby-ri ruby-irb ruby-rdoc </li></ul></ul><ul><ul><li>tar xzvf rubygems-1.3.1.tgz </li></ul></ul><ul><ul><li>cd rubygems </li></ul></ul><ul><ul><li>sudo ruby setup.rb </li></ul></ul><ul><ul><li>sudo gem update </li></ul></ul><ul><ul><li>sudo gem install rails </li></ul></ul><ul><li>Windows </li></ul><ul><ul><li>http://rubyonrails.org/download </li></ul></ul>
  19. 19. Gems installed on Server 123 <ul><li>[vuhung@vinicorp ~]$ sudo gem list </li></ul><ul><li>*** LOCAL GEMS *** </li></ul><ul><li>actionmailer (2.3.5, 2.2.2) </li></ul><ul><li>actionpack (2.3.5, 2.2.2) </li></ul><ul><li>activerecord (2.3.5, 2.2.2) </li></ul><ul><li>activeresource (2.3.5, 2.2.2) </li></ul><ul><li>activesupport (2.3.5, 2.2.2) </li></ul><ul><li>eventmachine (0.12.10) </li></ul><ul><li>fastthread (1.0.7) </li></ul><ul><li>htmlentities (4.2.0) </li></ul><ul><li>json (1.2.0) </li></ul><ul><li>juggernaut (0.5.8) </li></ul><ul><li>passenger (2.2.9) </li></ul><ul><li>rack (1.0.1) </li></ul><ul><li>rails (2.3.5, 2.2.2) </li></ul><ul><li>rake (0.8.7 </li></ul>
  20. 20. Rake <ul><li>A software building tool (automatically). </li></ul><ul><li>Written in Ruby. </li></ul><ul><li>Configuration file: Rakefiles, ruby syntax. </li></ul><ul><ul><li>Common task can be done by ruby blocks (make cannot). </li></ul></ul>Rake GNU make
  21. 21. Test-Driven Development <ul><li>Test first </li></ul><ul><ul><li>Write test-cases first. </li></ul></ul><ul><ul><li>Automatic test cases generation. </li></ul></ul><ul><li>Short development circle. </li></ul><ul><li>Regression test. </li></ul><ul><li>Automated unit test. </li></ul><ul><li>“Test” folder generated by Rake. </li></ul>
  22. 22. A short tutorial <ul><li>[vuhung@vinicorp ~]$ irb </li></ul><ul><li>irb(main):001:0> puts &quot;Hello Worlds&quot; </li></ul><ul><li>Hello Worlds </li></ul><ul><li>=> nil </li></ul><ul><li>irb(main):002:0> 3+2 </li></ul><ul><li>=> 5 </li></ul><ul><li>irb(main):003:0> 3*2 </li></ul><ul><li>=> 6 </li></ul><ul><li>irb(main):004:0> 3**2 </li></ul><ul><li>=> 9 </li></ul><ul><li>irb(main):005:0> Math.sqrt(9) </li></ul><ul><li>=> 3.0 </li></ul><ul><li>irb(main):006:0> a = 3**2 </li></ul><ul><li>=> 9 </li></ul><ul><li>irb(main):007:0> b = 4**2 </li></ul><ul><li>=> 16 </li></ul><ul><li>irb(main):008:0> Math.sqrt(a+b) </li></ul><ul><li>=> 5.0 </li></ul><ul><li>irb(main):009:0> </li></ul>
  23. 23. Redmine <ul><li>Web based Project Management Application. </li></ul><ul><li>Written in Ruby on Rails </li></ul><ul><li>GPL v2 licensed (free software). </li></ul><ul><li>Rake as build system. </li></ul><ul><li>Gems </li></ul><ul><li>WEBrick </li></ul><ul><li>Plenty of plugins available. </li></ul><ul><li>Stable since 2009/11 to date. </li></ul><ul><li>Supports various DB back-end after Rails. </li></ul>
  24. 24. Redmine Folder Structure (2) <ul><li>ls -1 /var/www/html/redmine/redmine-0.8.7 </li></ul><ul><li>app </li></ul><ul><li>config </li></ul><ul><li>db </li></ul><ul><li>doc </li></ul><ul><li>extra </li></ul><ul><li>files </li></ul><ul><li>lang </li></ul><ul><li>lib </li></ul><ul><li>log </li></ul><ul><li>public </li></ul><ul><li>Rakefile </li></ul><ul><li>script </li></ul><ul><li>start_redmine.sh </li></ul><ul><li>test </li></ul><ul><li>tmp </li></ul><ul><li>vendor </li></ul>
  25. 26. TODO <ul><li>http://www.railstutorial.org/book#top </li></ul><ul><li>http://www.railstutorial.org/chapters/a-demo-app#top </li></ul><ul><li>http://www.railstutorial.org/chapters/static-pages#top </li></ul><ul><li>http://www.railstutorial.org/chapters/rails-flavored-ruby#top </li></ul><ul><li>http://www.railstutorial.org/chapters/filling-in-the-layout#top </li></ul><ul><li>http://www.railstutorial.org/chapters/modeling-and-viewing-users-one#top </li></ul><ul><li>http://www.railstutorial.org/chapters/modeling-and-viewing-users-two#top </li></ul><ul><li>http://www.railstutorial.org/chapters/sign-up#top </li></ul><ul><li>http://www.railstutorial.org/chapters/sign-in-sign-out#top </li></ul><ul><li>http://www.railstutorial.org/chapters/updating-showing-and-deleting-users#top </li></ul>
  26. 27. TODO <ul><li>http://www-users.math.umd.edu/~dcarrera/ruby/0.3/chp_01/strings.html </li></ul><ul><li>http://www-users.math.umd.edu/~dcarrera/ruby/0.3/chp_01/strings.html </li></ul><ul><li>http://www-users.math.umd.edu/~dcarrera/ruby/0.3/chp_01/objects.html </li></ul><ul><li>http://www-users.math.umd.edu/~dcarrera/ruby/0.3/chp_01/variables.html </li></ul><ul><li>http://www-users.math.umd.edu/~dcarrera/ruby/0.3/chp_01/programs.html </li></ul><ul><li>http://www-users.math.umd.edu/~dcarrera/ruby/0.3/chp_01/tips.html </li></ul><ul><li>http://www-users.math.umd.edu/~dcarrera/ruby/0.3/chp_02/loops.html </li></ul><ul><li>http://www-users.math.umd.edu/~dcarrera/ruby/0.3/chp_02/user_input.html </li></ul><ul><li>http://www-users.math.umd.edu/~dcarrera/ruby/0.3/chp_02/conditionals.html </li></ul><ul><li>http://www-users.math.umd.edu/~dcarrera/ruby/0.3/chp_02/tips.html </li></ul><ul><li>http://www-users.math.umd.edu/~dcarrera/ruby/0.3/chp_03/arrays.html </li></ul><ul><li>http://www-users.math.umd.edu/~dcarrera/ruby/0.3/chp_03/iterators.html </li></ul><ul><li>http://www-users.math.umd.edu/~dcarrera/ruby/0.3/chp_03/hashes.html </li></ul><ul><li>http://www-users.math.umd.edu/~dcarrera/ruby/0.3/chp_03/address.html </li></ul><ul><li>http://www-users.math.umd.edu/~dcarrera/ruby/0.3/chp_03/address2.html </li></ul><ul><li>http://www-users.math.umd.edu/~dcarrera/ruby/0.3/chp_03/sorting.html </li></ul><ul><li>http://www-users.math.umd.edu/~dcarrera/ruby/0.3/chp_04/classes.html </li></ul><ul><li>http://www-users.math.umd.edu/~dcarrera/ruby/0.3/chp_03/address2.html </li></ul><ul><li>http://www-users.math.umd.edu/~dcarrera/ruby/0.3/chp_03/sorting.html </li></ul><ul><li>http://www-users.math.umd.edu/~dcarrera/ruby/0.3/chp_04/functions.html </li></ul><ul><li>http://www-users.math.umd.edu/~dcarrera/ruby/0.3/chp_04/classes.html </li></ul><ul><li>http://www-users.math.umd.edu/~dcarrera/ruby/0.3/chp_04/classes2.html </li></ul><ul><li>http://www-users.math.umd.edu/~dcarrera/ruby/0.3/chp_04/addressbook.html </li></ul><ul><li>http://www-users.math.umd.edu/~dcarrera/ruby/0.3/chp_04/features.html </li></ul>
  27. 28. References <ul><li>http://en.wikipedia.org/wiki/Rapid_application_development </li></ul><ul><li>http://en.wikipedia.org/wiki/Agile_software_development </li></ul><ul><li>http://en.wikipedia.org/wiki/Yukihiro_Matsumoto </li></ul><ul><li>http://en.wikipedia.org/wiki/Ruby_%28programming_language%29#Examples </li></ul><ul><li>http://en.wikipedia.org/wiki/Ruby_%28programming_language%29 </li></ul><ul><li>http://en.wikipedia.org/wiki/Ruby_on_Rails </li></ul><ul><li>http://www.google.com/webhp?hl=en#hl=en&source=hp&q=ruby+functional+programming&aq=f&aqi=&aql=&oq=&gs_rfai=&fp=a86c207b1c79523e </li></ul><ul><li>http://stackoverflow.com/questions/159797/is-ruby-a-functional-language </li></ul><ul><li>http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/330387 </li></ul><ul><li>http://stackoverflow.com/questions/546968/when-you-say-ruby-is-reflective-does-this-mainly-refer-to-duck-typing </li></ul><ul><li>http://www.tutorialspoint.com/ruby/ruby_object_oriented.htm </li></ul><ul><li>http://www.rubyist.net/~slagell/ruby/oothinking.html </li></ul><ul><li>http://www.techotopia.com/index.php/Ruby_Object_Oriented_Programming </li></ul><ul><li>http://d.hatena.ne.jp/shunsuk/20090101/1230816826 </li></ul><ul><li>http://www.slideshare.net/peter_marklund/ruby-on-rails-101-presentation-slides-for-a-five-day-introductory-course </li></ul><ul><li>http://picasaweb.google.com/Dikiwinky/Ruby#5116531304417868130 </li></ul><ul><li>http://articles.slicehost.com/2009/4/7/centos-ruby-on-rails </li></ul><ul><li>http://www.railstutorial.org/book </li></ul>

×