Download presentation
Presentation is loading. Please wait.
Published byBarbra Curtis Modified over 9 years ago
1
Ruby on Rails 101.2
2
WTF? Created by Matz in 1993 Inspired by Smalltalk Rubygems = CSPAN Rake = ant Rdoc = javadoc
3
http://rubyurl.com/D1LA
4
How its like Java Object orientated Supports single inheritance Has exceptions Runs on multiple OS’s Runs on Java runtime (using jruby)
5
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)
6
When might you use it?
7
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. http://www.theserverside.com/news/thread.tss?thread_id=33120
8
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. http://www.theserverside.com/news/thread.tss?thread_id=33120 Convention over configuration
9
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/fin al/ http://subversion.tigris.org/project_packages. html
10
Resources http://groups.google.com/group/ruby_ire land/web/rails-resourceshttp://groups.google.com/group/ruby_ire land/web/rails-resources http://railscasts.com/ http://www.railsenvy.com/ http://tinyurl.com/ytgp8d ;)http://tinyurl.com/ytgp8d
11
What we’ll cover :symbols Passing blocks Creating objects Closures
12
:symbols
13
“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
14
Passing blocks (goal posts)
15
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 I end Sans goalpoasts Avec goalpoasts
16
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
17
Hashes myHash = { :name => “James”, :occupation => “Programmer” } myHash.each { |i, k| puts “#{i}:#{k}” }
18
Everything is an object
19
class James initialize puts “initializing” @x = 3 # defines a member variable end # provides an accessor def x @x end # set x from outisde of the def x= arg @x = arg end http://innig.net/software/ruby/closures-in-ruby.rb
20
class Person attr_accessor :second_name initialize puts “initializing” @first_name = 3 # defines member variable end http://innig.net/software/ruby/closures-in-ruby.rb
21
~ jkennedy$ irb irb(main):001:0> require 'test' => true irb(main):002:0> j = James.new initialising => # irb(main):003:0> j.x => 3 irb(main):004:0> j.x= 5 => 5 irb(main):005:0> j.x => 5 irb(main):006:0>
22
Exceptions
23
class RaisingExceptionionator def initialize begin file = open “some_file” rescue file = STDIN retry # error? end
24
Closures
25
# Example 1: def thrice yield end x = 5 puts "value of x before: #{x}" thrice { x += 1 } puts "value of x after: #{x}" http://innig.net/software/ruby/closures-in-ruby.rb
26
# Example 2: local scope def thrice_with_local_x x = 100 yield puts "value of x at end of thrice_with_local_x: #{x}" end x = 5 thrice_with_local_x { x += 1 } puts "value of outer x after: #{x}" http://innig.net/software/ruby/closures-in-ruby.rb
27
# 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? y end puts "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
28
#Example 4: yield and scope def six_times(&block) thrice(&block) end x = 4 six_times { x += 10 } puts "value of x after: #{x}" http://innig.net/software/ruby/closures-in-ruby.rb
29
# 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
30
# Example 5 : … continued save_for_later { puts "Hello!" } puts "Deferred execution of a block:" @saved.call http://innig.net/software/ruby/closures-in-ruby.rb
31
@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
32
Modules Modules cannot be instantiated Define a set of ‘behaviours’ which an object can ‘inherit’ Module Politeness def say_hello puts “Hello” end
33
class Ambassador def initialize require ‘Politeness’ end kofi = Ambassador.new kofi.say_hello
34
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
35
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 rails Imports the rails Code into your app
37
Sqllite :script/generate model Person name:string phone:integer biography:text :rake db:create :rake db:migrate :sqllite3 db/development.sqllite3 sqllite>.tables
38
Anatomy of a project Global configuration Methods available in views Active Record Classes * Templates Active Record Classes * URL => controller configuration
39
CRUD Create => [ new, update] Read => [ list, show] Update => [edit, update] Delete => [destroy]
40
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
41
Read rake rails:freeze:gems def index @items = Item.find :all end def show @item = Item.find params[:id] end
42
Update rake rails:freeze:gems def edit @item = Item.find params[:id] end def show @item = Item.find params[:id] end
43
Delete rake rails:freeze:gems def edit @item = Item.find params[:id] @item.destory end
44
Adding Authentication./script/plugin install http://svn.techno- weenie.net/projects/plugins/restful_authenti cation/./script/generate authenticated user sessions Active Record Session only
45
Setting site template #{APP_ROOT}/app/views/layout/#{mod el_name}.erb renders content
46
Get a template Google: ‘yui builder’
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.