Ruby on Rails 101.2. WTF? Created by Matz in 1993 Inspired by Smalltalk Rubygems = CSPAN Rake = ant...
-
Upload
barbra-curtis -
Category
Documents
-
view
215 -
download
0
Transcript of Ruby on Rails 101.2. WTF? Created by Matz in 1993 Inspired by Smalltalk Rubygems = CSPAN Rake = ant...
![Page 1: Ruby on Rails 101.2. WTF? Created by Matz in 1993 Inspired by Smalltalk Rubygems = CSPAN Rake = ant Rdoc = javadoc.](https://reader035.fdocuments.us/reader035/viewer/2022062518/56649efb5503460f94c0d1c9/html5/thumbnails/1.jpg)
Ruby on Rails 101.2
![Page 2: Ruby on Rails 101.2. WTF? Created by Matz in 1993 Inspired by Smalltalk Rubygems = CSPAN Rake = ant Rdoc = javadoc.](https://reader035.fdocuments.us/reader035/viewer/2022062518/56649efb5503460f94c0d1c9/html5/thumbnails/2.jpg)
WTF?
• Created by Matz in 1993
• Inspired by Smalltalk
• Rubygems = CSPAN
• Rake = ant
• Rdoc = javadoc
![Page 3: Ruby on Rails 101.2. WTF? Created by Matz in 1993 Inspired by Smalltalk Rubygems = CSPAN Rake = ant Rdoc = javadoc.](https://reader035.fdocuments.us/reader035/viewer/2022062518/56649efb5503460f94c0d1c9/html5/thumbnails/3.jpg)
http://rubyurl.com/D1LA
QuickTime™ and aTIFF (PackBits) decompressorare needed to see this picture.
![Page 4: Ruby on Rails 101.2. WTF? Created by Matz in 1993 Inspired by Smalltalk Rubygems = CSPAN Rake = ant Rdoc = javadoc.](https://reader035.fdocuments.us/reader035/viewer/2022062518/56649efb5503460f94c0d1c9/html5/thumbnails/4.jpg)
How its like Java
• Object orientated
• Supports single inheritance
• Has exceptions
• Runs on multiple OS’s
• Runs on Java runtime (using jruby)
![Page 5: Ruby on Rails 101.2. WTF? Created by Matz in 1993 Inspired by Smalltalk Rubygems = CSPAN Rake = ant Rdoc = javadoc.](https://reader035.fdocuments.us/reader035/viewer/2022062518/56649efb5503460f94c0d1c9/html5/thumbnails/5.jpg)
How it differs
• Loosely typed until assigned
• Supports closures / passing blocks
• Supports ‘modular’ design (mixins)
• Poor language level support for mulit-threaded execution
• No primitives (int, float etc)
![Page 6: Ruby on Rails 101.2. WTF? Created by Matz in 1993 Inspired by Smalltalk Rubygems = CSPAN Rake = ant Rdoc = javadoc.](https://reader035.fdocuments.us/reader035/viewer/2022062518/56649efb5503460f94c0d1c9/html5/thumbnails/6.jpg)
When might you use it?
![Page 7: Ruby on Rails 101.2. WTF? Created by Matz in 1993 Inspired by Smalltalk Rubygems = CSPAN Rake = ant Rdoc = javadoc.](https://reader035.fdocuments.us/reader035/viewer/2022062518/56649efb5503460f94c0d1c9/html5/thumbnails/7.jpg)
YARJC: Yet another Rails vs. Java Comparison
• Justin Gehtland recently tried re-implementing one of his web applications in Ruby on Rails instead of his normal Java Spring/Hibernate setup, according to a Slashdot post.
QuickTime™ and aTIFF (PackBits) decompressorare needed to see this picture.
http://www.theserverside.com/news/thread.tss?thread_id=33120
![Page 8: Ruby on Rails 101.2. WTF? Created by Matz in 1993 Inspired by Smalltalk Rubygems = CSPAN Rake = ant Rdoc = javadoc.](https://reader035.fdocuments.us/reader035/viewer/2022062518/56649efb5503460f94c0d1c9/html5/thumbnails/8.jpg)
YARJC: Yet another Rails vs. Java Comparison
• Justin Gehtland recently tried re-implementing one of his web applications in Ruby on Rails instead of his normal Java Spring/Hibernate setup, according to a Slashdot post.
QuickTime™ and aTIFF (PackBits) decompressorare needed to see this picture.
http://www.theserverside.com/news/thread.tss?thread_id=33120
Convention over configuration
![Page 9: Ruby on Rails 101.2. WTF? Created by Matz in 1993 Inspired by Smalltalk Rubygems = CSPAN Rake = ant Rdoc = javadoc.](https://reader035.fdocuments.us/reader035/viewer/2022062518/56649efb5503460f94c0d1c9/html5/thumbnails/9.jpg)
Setup
• http://www.rubyonrails.org/down• http://www.sqlite.org/
– The version that comes with Leopard isn't high enough. You'll need to get he source and make / sudo make install. Then run 'gem install sqlite3-ruby' (all on the mac). The binaries should do for windows.
• http://download.netbeans.org/netbeans/6.0/final/ http://subversion.tigris.org/project_packages.html
![Page 10: Ruby on Rails 101.2. WTF? Created by Matz in 1993 Inspired by Smalltalk Rubygems = CSPAN Rake = ant Rdoc = javadoc.](https://reader035.fdocuments.us/reader035/viewer/2022062518/56649efb5503460f94c0d1c9/html5/thumbnails/10.jpg)
Resources
• http://groups.google.com/group/ruby_ireland/web/rails-resources
• http://railscasts.com/
• http://www.railsenvy.com/
• http://tinyurl.com/ytgp8d ;)
![Page 11: Ruby on Rails 101.2. WTF? Created by Matz in 1993 Inspired by Smalltalk Rubygems = CSPAN Rake = ant Rdoc = javadoc.](https://reader035.fdocuments.us/reader035/viewer/2022062518/56649efb5503460f94c0d1c9/html5/thumbnails/11.jpg)
What we’ll cover
• :symbols
• Passing blocks
• Creating objects
• Closures
![Page 12: Ruby on Rails 101.2. WTF? Created by Matz in 1993 Inspired by Smalltalk Rubygems = CSPAN Rake = ant Rdoc = javadoc.](https://reader035.fdocuments.us/reader035/viewer/2022062518/56649efb5503460f94c0d1c9/html5/thumbnails/12.jpg)
:symbols
![Page 13: Ruby on Rails 101.2. WTF? Created by Matz in 1993 Inspired by Smalltalk Rubygems = CSPAN Rake = ant Rdoc = javadoc.](https://reader035.fdocuments.us/reader035/viewer/2022062518/56649efb5503460f94c0d1c9/html5/thumbnails/13.jpg)
“a”.intern = :a
Symbols are ‘weird string’s• Immutable (cannot be reassigned)• Used internally to represent language elements
• eg: 100 would have a symbol assocaited with it• Commonly used in hash maps as keys
![Page 14: Ruby on Rails 101.2. WTF? Created by Matz in 1993 Inspired by Smalltalk Rubygems = CSPAN Rake = ant Rdoc = javadoc.](https://reader035.fdocuments.us/reader035/viewer/2022062518/56649efb5503460f94c0d1c9/html5/thumbnails/14.jpg)
Passing blocks (goal posts)
![Page 15: Ruby on Rails 101.2. WTF? Created by Matz in 1993 Inspired by Smalltalk Rubygems = CSPAN Rake = ant Rdoc = javadoc.](https://reader035.fdocuments.us/reader035/viewer/2022062518/56649efb5503460f94c0d1c9/html5/thumbnails/15.jpg)
3.times do puts “Hello world”End
http://innig.net/software/ruby/closures-in-ruby.rb
myArray = Array.new [3, 4, 3, 9]myArray.each do |i|puts Iend
Sans goalpoasts
Avec goalpoasts
![Page 16: Ruby on Rails 101.2. WTF? Created by Matz in 1993 Inspired by Smalltalk Rubygems = CSPAN Rake = ant Rdoc = javadoc.](https://reader035.fdocuments.us/reader035/viewer/2022062518/56649efb5503460f94c0d1c9/html5/thumbnails/16.jpg)
3.times { puts “Hello world”}
http://innig.net/software/ruby/closures-in-ruby.rb
myArray = Array.new [3, 4, 3, 9]myArray.each { |i|puts i}
Sans goalpoasts
Avec goalpoasts
![Page 17: Ruby on Rails 101.2. WTF? Created by Matz in 1993 Inspired by Smalltalk Rubygems = CSPAN Rake = ant Rdoc = javadoc.](https://reader035.fdocuments.us/reader035/viewer/2022062518/56649efb5503460f94c0d1c9/html5/thumbnails/17.jpg)
Hashes
myHash = { :name => “James”, :occupation => “Programmer” } myHash.each { |i, k| puts “#{i}:#{k}”}
![Page 18: Ruby on Rails 101.2. WTF? Created by Matz in 1993 Inspired by Smalltalk Rubygems = CSPAN Rake = ant Rdoc = javadoc.](https://reader035.fdocuments.us/reader035/viewer/2022062518/56649efb5503460f94c0d1c9/html5/thumbnails/18.jpg)
Everything is an object
![Page 19: Ruby on Rails 101.2. WTF? Created by Matz in 1993 Inspired by Smalltalk Rubygems = CSPAN Rake = ant Rdoc = javadoc.](https://reader035.fdocuments.us/reader035/viewer/2022062518/56649efb5503460f94c0d1c9/html5/thumbnails/19.jpg)
class James
initializeputs “initializing”@x = 3 # defines a member variable
end
# provides an accessordef x
@xend# set x from outisde of the def x= arg
@x = argend
endhttp://innig.net/software/ruby/closures-in-ruby.rb
![Page 20: Ruby on Rails 101.2. WTF? Created by Matz in 1993 Inspired by Smalltalk Rubygems = CSPAN Rake = ant Rdoc = javadoc.](https://reader035.fdocuments.us/reader035/viewer/2022062518/56649efb5503460f94c0d1c9/html5/thumbnails/20.jpg)
class Personattr_accessor :second_nameinitialize
puts “initializing”@first_name = 3 # defines member
variable end
end
http://innig.net/software/ruby/closures-in-ruby.rb
![Page 21: Ruby on Rails 101.2. WTF? Created by Matz in 1993 Inspired by Smalltalk Rubygems = CSPAN Rake = ant Rdoc = javadoc.](https://reader035.fdocuments.us/reader035/viewer/2022062518/56649efb5503460f94c0d1c9/html5/thumbnails/21.jpg)
~ jkennedy$ irbirb(main):001:0> require 'test'=> trueirb(main):002:0> j = James.newinitialising=> #<James:0x1cc8e4 @x=3>irb(main):003:0> j.x=> 3irb(main):004:0> j.x= 5=> 5irb(main):005:0> j.x=> 5irb(main):006:0>
![Page 22: Ruby on Rails 101.2. WTF? Created by Matz in 1993 Inspired by Smalltalk Rubygems = CSPAN Rake = ant Rdoc = javadoc.](https://reader035.fdocuments.us/reader035/viewer/2022062518/56649efb5503460f94c0d1c9/html5/thumbnails/22.jpg)
Exceptions
![Page 23: Ruby on Rails 101.2. WTF? Created by Matz in 1993 Inspired by Smalltalk Rubygems = CSPAN Rake = ant Rdoc = javadoc.](https://reader035.fdocuments.us/reader035/viewer/2022062518/56649efb5503460f94c0d1c9/html5/thumbnails/23.jpg)
class RaisingExceptionionator
def initialize begin
file = open “some_file” rescue
file = STDINretry # error?
endend
end
![Page 24: Ruby on Rails 101.2. WTF? Created by Matz in 1993 Inspired by Smalltalk Rubygems = CSPAN Rake = ant Rdoc = javadoc.](https://reader035.fdocuments.us/reader035/viewer/2022062518/56649efb5503460f94c0d1c9/html5/thumbnails/24.jpg)
Closures
![Page 25: Ruby on Rails 101.2. WTF? Created by Matz in 1993 Inspired by Smalltalk Rubygems = CSPAN Rake = ant Rdoc = javadoc.](https://reader035.fdocuments.us/reader035/viewer/2022062518/56649efb5503460f94c0d1c9/html5/thumbnails/25.jpg)
# Example 1: def thrice yield yield yieldend
x = 5puts "value of x before: #{x}"thrice { x += 1 }puts "value of x after: #{x}"
http://innig.net/software/ruby/closures-in-ruby.rb
![Page 26: Ruby on Rails 101.2. WTF? Created by Matz in 1993 Inspired by Smalltalk Rubygems = CSPAN Rake = ant Rdoc = javadoc.](https://reader035.fdocuments.us/reader035/viewer/2022062518/56649efb5503460f94c0d1c9/html5/thumbnails/26.jpg)
# Example 2: local scopedef thrice_with_local_x x = 100 yield yield yield puts "value of x at end of thrice_with_local_x: #{x}"end
x = 5thrice_with_local_x { x += 1 }puts "value of outer x after: #{x}"
http://innig.net/software/ruby/closures-in-ruby.rb
![Page 27: Ruby on Rails 101.2. WTF? Created by Matz in 1993 Inspired by Smalltalk Rubygems = CSPAN Rake = ant Rdoc = javadoc.](https://reader035.fdocuments.us/reader035/viewer/2022062518/56649efb5503460f94c0d1c9/html5/thumbnails/27.jpg)
# Example 3: thrice do # note that {...} and do...end are equivalent y = 10 puts "Is y defined inside the block where it is first set?" puts "Yes." if defined? yendputs "Is y defined in the outer context after being set in the block?"puts "No!" unless defined? y
http://innig.net/software/ruby/closures-in-ruby.rb
![Page 28: Ruby on Rails 101.2. WTF? Created by Matz in 1993 Inspired by Smalltalk Rubygems = CSPAN Rake = ant Rdoc = javadoc.](https://reader035.fdocuments.us/reader035/viewer/2022062518/56649efb5503460f94c0d1c9/html5/thumbnails/28.jpg)
#Example 4: yield and scopedef six_times(&block) thrice(&block) thrice(&block)end
x = 4six_times { x += 10 }puts "value of x after: #{x}"
http://innig.net/software/ruby/closures-in-ruby.rb
![Page 29: Ruby on Rails 101.2. WTF? Created by Matz in 1993 Inspired by Smalltalk Rubygems = CSPAN Rake = ant Rdoc = javadoc.](https://reader035.fdocuments.us/reader035/viewer/2022062518/56649efb5503460f94c0d1c9/html5/thumbnails/29.jpg)
# Example 5# So do we have closures? Not quite! We can't hold on #to a &block and call it later at an arbitrary# time; it doesn't work. This, for example, will not #compile:## def save_block_for_later(&block)# saved = █# end## But we *can* pass it around if we use drop the &, and use block.call(...) instead of yield:
def save_for_later(&b) @saved = b # Note: no ampersand! This turns a block into a closure of sorts.end http://innig.net/software/ruby/closures-in-ruby.rb
![Page 30: Ruby on Rails 101.2. WTF? Created by Matz in 1993 Inspired by Smalltalk Rubygems = CSPAN Rake = ant Rdoc = javadoc.](https://reader035.fdocuments.us/reader035/viewer/2022062518/56649efb5503460f94c0d1c9/html5/thumbnails/30.jpg)
# Example 5 : … continued
save_for_later { puts "Hello!" }puts "Deferred execution of a block:"@[email protected]
http://innig.net/software/ruby/closures-in-ruby.rb
![Page 31: Ruby on Rails 101.2. WTF? Created by Matz in 1993 Inspired by Smalltalk Rubygems = CSPAN Rake = ant Rdoc = javadoc.](https://reader035.fdocuments.us/reader035/viewer/2022062518/56649efb5503460f94c0d1c9/html5/thumbnails/31.jpg)
@saved_proc_new = Proc.new { puts "I'm declared with Proc.new." }@saved_proc = proc { puts "I'm declared with proc." }@saved_lambda = lambda { puts "I'm declared with lambda." }def some_method puts "I'm declared as a method."end@method_as_closure = method(:some_method)
puts "Here are four superficially identical forms of deferred execution:"@saved_proc_new.call@saved_proc.call@saved_lambda.call@method_as_closure.call
http://innig.net/software/ruby/closures-in-ruby.rb
![Page 32: Ruby on Rails 101.2. WTF? Created by Matz in 1993 Inspired by Smalltalk Rubygems = CSPAN Rake = ant Rdoc = javadoc.](https://reader035.fdocuments.us/reader035/viewer/2022062518/56649efb5503460f94c0d1c9/html5/thumbnails/32.jpg)
Modules
• Modules cannot be instantiated
• Define a set of ‘behaviours’ which an object can ‘inherit’
Module Politeness
def say_helloputs “Hello”
endend
![Page 33: Ruby on Rails 101.2. WTF? Created by Matz in 1993 Inspired by Smalltalk Rubygems = CSPAN Rake = ant Rdoc = javadoc.](https://reader035.fdocuments.us/reader035/viewer/2022062518/56649efb5503460f94c0d1c9/html5/thumbnails/33.jpg)
class Ambassador
def initializerequire ‘Politeness’
end
end
kofi = Ambassador.newkofi.say_hello
![Page 34: Ruby on Rails 101.2. WTF? Created by Matz in 1993 Inspired by Smalltalk Rubygems = CSPAN Rake = ant Rdoc = javadoc.](https://reader035.fdocuments.us/reader035/viewer/2022062518/56649efb5503460f94c0d1c9/html5/thumbnails/34.jpg)
Rails 2.0.2: rails myfirstproject: cd myfirstproject: rake rails:freeze:gems: script/generate - -help: script/destroy - -help
: script/migration - -help
: script/resource - -help
: script/scaffold - -help
![Page 35: Ruby on Rails 101.2. WTF? Created by Matz in 1993 Inspired by Smalltalk Rubygems = CSPAN Rake = ant Rdoc = javadoc.](https://reader035.fdocuments.us/reader035/viewer/2022062518/56649efb5503460f94c0d1c9/html5/thumbnails/35.jpg)
Rails 2.0.2: rails myfirstproject: cd myfirstproject: rake rails:freeze:gems: script/generate - -help: script/destroy - -help
: script/migration - -help
: script/resource - -help
: script/scaffold - -help
Freezing railsImports the rails
Code into your app
![Page 36: Ruby on Rails 101.2. WTF? Created by Matz in 1993 Inspired by Smalltalk Rubygems = CSPAN Rake = ant Rdoc = javadoc.](https://reader035.fdocuments.us/reader035/viewer/2022062518/56649efb5503460f94c0d1c9/html5/thumbnails/36.jpg)
QuickTime™ and aTIFF (LZW) decompressor
are needed to see this picture.
![Page 37: Ruby on Rails 101.2. WTF? Created by Matz in 1993 Inspired by Smalltalk Rubygems = CSPAN Rake = ant Rdoc = javadoc.](https://reader035.fdocuments.us/reader035/viewer/2022062518/56649efb5503460f94c0d1c9/html5/thumbnails/37.jpg)
Sqllite
:script/generate model Person name:string phone:integer biography:text:rake db:create
:rake db:migrate:sqllite3 db/development.sqllite3sqllite>.tables
![Page 38: Ruby on Rails 101.2. WTF? Created by Matz in 1993 Inspired by Smalltalk Rubygems = CSPAN Rake = ant Rdoc = javadoc.](https://reader035.fdocuments.us/reader035/viewer/2022062518/56649efb5503460f94c0d1c9/html5/thumbnails/38.jpg)
Anatomy of a project
QuickTime™ and aTIFF (PackBits) decompressorare needed to see this picture.
Global configuration
Methods available in views
Active Record Classes *
Templates
Active Record Classes *
URL => controller configuration
![Page 39: Ruby on Rails 101.2. WTF? Created by Matz in 1993 Inspired by Smalltalk Rubygems = CSPAN Rake = ant Rdoc = javadoc.](https://reader035.fdocuments.us/reader035/viewer/2022062518/56649efb5503460f94c0d1c9/html5/thumbnails/39.jpg)
CRUD
• Create => [ new, update]
• Read => [ list, show]
• Update => [edit, update]
• Delete => [destroy]
![Page 40: Ruby on Rails 101.2. WTF? Created by Matz in 1993 Inspired by Smalltalk Rubygems = CSPAN Rake = ant Rdoc = javadoc.](https://reader035.fdocuments.us/reader035/viewer/2022062518/56649efb5503460f94c0d1c9/html5/thumbnails/40.jpg)
Create
rake rails:freeze:gems
def new@item = Item.new
end
def update@item = Item.new params[:item]if @item.save
flash[:notice] = ‘Item was created’else
render :action => “new”end
end
![Page 41: Ruby on Rails 101.2. WTF? Created by Matz in 1993 Inspired by Smalltalk Rubygems = CSPAN Rake = ant Rdoc = javadoc.](https://reader035.fdocuments.us/reader035/viewer/2022062518/56649efb5503460f94c0d1c9/html5/thumbnails/41.jpg)
Read
rake rails:freeze:gems
def index@items = Item.find :all
end
def show@item = Item.find params[:id]
end
![Page 42: Ruby on Rails 101.2. WTF? Created by Matz in 1993 Inspired by Smalltalk Rubygems = CSPAN Rake = ant Rdoc = javadoc.](https://reader035.fdocuments.us/reader035/viewer/2022062518/56649efb5503460f94c0d1c9/html5/thumbnails/42.jpg)
Update
rake rails:freeze:gems
def edit@item = Item.find params[:id]
end
def show@item = Item.find params[:id]
end
![Page 43: Ruby on Rails 101.2. WTF? Created by Matz in 1993 Inspired by Smalltalk Rubygems = CSPAN Rake = ant Rdoc = javadoc.](https://reader035.fdocuments.us/reader035/viewer/2022062518/56649efb5503460f94c0d1c9/html5/thumbnails/43.jpg)
Delete
rake rails:freeze:gems
def edit@item = Item.find params[:id]@item.destory
end
![Page 44: Ruby on Rails 101.2. WTF? Created by Matz in 1993 Inspired by Smalltalk Rubygems = CSPAN Rake = ant Rdoc = javadoc.](https://reader035.fdocuments.us/reader035/viewer/2022062518/56649efb5503460f94c0d1c9/html5/thumbnails/44.jpg)
Adding Authentication
./script/plugin install http://svn.techno-weenie.net/projects/plugins/restful_authentication/
./script/generate authenticated user sessions
ActiveRecordSession
only
![Page 45: Ruby on Rails 101.2. WTF? Created by Matz in 1993 Inspired by Smalltalk Rubygems = CSPAN Rake = ant Rdoc = javadoc.](https://reader035.fdocuments.us/reader035/viewer/2022062518/56649efb5503460f94c0d1c9/html5/thumbnails/45.jpg)
Setting site template
• #{APP_ROOT}/app/views/layout/#{model_name}.erb
• <%= yield %> renders content
![Page 46: Ruby on Rails 101.2. WTF? Created by Matz in 1993 Inspired by Smalltalk Rubygems = CSPAN Rake = ant Rdoc = javadoc.](https://reader035.fdocuments.us/reader035/viewer/2022062518/56649efb5503460f94c0d1c9/html5/thumbnails/46.jpg)
Get a template
• Google: ‘yui builder’
<%= stylesheet_link_tag 'application' %>
<%= javascript_include_tag :defaults %>