AITP Ruby And Rails Talk
-
Upload
jasondew -
Category
Technology
-
view
1.280 -
download
3
description
Transcript of AITP Ruby And Rails Talk
![Page 1: AITP Ruby And Rails Talk](https://reader030.fdocuments.us/reader030/viewer/2022020207/54b94f694a7959702e8b470b/html5/thumbnails/1.jpg)
Ruby and RailsJason Dew
![Page 2: AITP Ruby And Rails Talk](https://reader030.fdocuments.us/reader030/viewer/2022020207/54b94f694a7959702e8b470b/html5/thumbnails/2.jpg)
Ruby
![Page 3: AITP Ruby And Rails Talk](https://reader030.fdocuments.us/reader030/viewer/2022020207/54b94f694a7959702e8b470b/html5/thumbnails/3.jpg)
pure OO
![Page 4: AITP Ruby And Rails Talk](https://reader030.fdocuments.us/reader030/viewer/2022020207/54b94f694a7959702e8b470b/html5/thumbnails/4.jpg)
42.methods.sort
![Page 5: AITP Ruby And Rails Talk](https://reader030.fdocuments.us/reader030/viewer/2022020207/54b94f694a7959702e8b470b/html5/thumbnails/5.jpg)
beautiful
![Page 6: AITP Ruby And Rails Talk](https://reader030.fdocuments.us/reader030/viewer/2022020207/54b94f694a7959702e8b470b/html5/thumbnails/6.jpg)
def fib n return 0 unless n > 0 return 1 if [1,2].include? n fib(n-1) + fib(n-2)end
puts fib(10) # => 55
![Page 7: AITP Ruby And Rails Talk](https://reader030.fdocuments.us/reader030/viewer/2022020207/54b94f694a7959702e8b470b/html5/thumbnails/7.jpg)
Symbols
![Page 8: AITP Ruby And Rails Talk](https://reader030.fdocuments.us/reader030/viewer/2022020207/54b94f694a7959702e8b470b/html5/thumbnails/8.jpg)
roygbiv = [:red, :orange, :yellow, :green, :blue, :indigo, :violet]
some_hash = { :bar => "bar", :life => 42 }
![Page 9: AITP Ruby And Rails Talk](https://reader030.fdocuments.us/reader030/viewer/2022020207/54b94f694a7959702e8b470b/html5/thumbnails/9.jpg)
blocks
![Page 10: AITP Ruby And Rails Talk](https://reader030.fdocuments.us/reader030/viewer/2022020207/54b94f694a7959702e8b470b/html5/thumbnails/10.jpg)
array = [42, "bar", ["x"]]
result = array.map do |element| element * 2end
puts result.inspect# => [84, "barbar", ["x", "x"]]
![Page 11: AITP Ruby And Rails Talk](https://reader030.fdocuments.us/reader030/viewer/2022020207/54b94f694a7959702e8b470b/html5/thumbnails/11.jpg)
classes
![Page 12: AITP Ruby And Rails Talk](https://reader030.fdocuments.us/reader030/viewer/2022020207/54b94f694a7959702e8b470b/html5/thumbnails/12.jpg)
class Monster def initialize @hitpoints = 400 end
def hitpoints @hitpoints end
end
dragon = Monster.newdragon.hitpoints # => 400
![Page 13: AITP Ruby And Rails Talk](https://reader030.fdocuments.us/reader030/viewer/2022020207/54b94f694a7959702e8b470b/html5/thumbnails/13.jpg)
modules
![Page 14: AITP Ruby And Rails Talk](https://reader030.fdocuments.us/reader030/viewer/2022020207/54b94f694a7959702e8b470b/html5/thumbnails/14.jpg)
module Bar def bar "baz" endend
include Barputs bar # => "baz"
![Page 15: AITP Ruby And Rails Talk](https://reader030.fdocuments.us/reader030/viewer/2022020207/54b94f694a7959702e8b470b/html5/thumbnails/15.jpg)
open classes
![Page 16: AITP Ruby And Rails Talk](https://reader030.fdocuments.us/reader030/viewer/2022020207/54b94f694a7959702e8b470b/html5/thumbnails/16.jpg)
class Array
def sum_of_squares inject(0) do |ss, element| ss + element ** 2 end end
end
[2,4,8,1,13,7].sum_of_squares # => 303
![Page 17: AITP Ruby And Rails Talk](https://reader030.fdocuments.us/reader030/viewer/2022020207/54b94f694a7959702e8b470b/html5/thumbnails/17.jpg)
define_method
![Page 18: AITP Ruby And Rails Talk](https://reader030.fdocuments.us/reader030/viewer/2022020207/54b94f694a7959702e8b470b/html5/thumbnails/18.jpg)
class Player
[:dexterity, :piety].each do |attribute| define_method(attribute) do puts "returning your #{attribute}" end
define_method("#{attribute}=") do |value| puts "setting #{attribute} to #{value}" end end
end
![Page 19: AITP Ruby And Rails Talk](https://reader030.fdocuments.us/reader030/viewer/2022020207/54b94f694a7959702e8b470b/html5/thumbnails/19.jpg)
my = Player.newmy.dexterity # => returning your dexteritymy.piety = 3 # => setting piety to 3
![Page 20: AITP Ruby And Rails Talk](https://reader030.fdocuments.us/reader030/viewer/2022020207/54b94f694a7959702e8b470b/html5/thumbnails/20.jpg)
method_missing
![Page 21: AITP Ruby And Rails Talk](https://reader030.fdocuments.us/reader030/viewer/2022020207/54b94f694a7959702e8b470b/html5/thumbnails/21.jpg)
class Caster
def sissy_slap puts "your enemy laughs" end
def method_missing name, *args puts "#{name}: spell unknown" end
end
me = Caster.newme.sissy_slap # => "your enemy laughs"me.fireball # => "fireball: spell unknown"
![Page 22: AITP Ruby And Rails Talk](https://reader030.fdocuments.us/reader030/viewer/2022020207/54b94f694a7959702e8b470b/html5/thumbnails/22.jpg)
singleton classes
![Page 23: AITP Ruby And Rails Talk](https://reader030.fdocuments.us/reader030/viewer/2022020207/54b94f694a7959702e8b470b/html5/thumbnails/23.jpg)
class Barend
bar = Bar.new
def bar.bar "baz"end
bar.bar # => "baz"
![Page 24: AITP Ruby And Rails Talk](https://reader030.fdocuments.us/reader030/viewer/2022020207/54b94f694a7959702e8b470b/html5/thumbnails/24.jpg)
Rails
![Page 25: AITP Ruby And Rails Talk](https://reader030.fdocuments.us/reader030/viewer/2022020207/54b94f694a7959702e8b470b/html5/thumbnails/25.jpg)
RESTful routing
![Page 26: AITP Ruby And Rails Talk](https://reader030.fdocuments.us/reader030/viewer/2022020207/54b94f694a7959702e8b470b/html5/thumbnails/26.jpg)
# config/routes.rb
ActionController::Routing::Routes.draw do |map| map.resources :barsend
![Page 27: AITP Ruby And Rails Talk](https://reader030.fdocuments.us/reader030/viewer/2022020207/54b94f694a7959702e8b470b/html5/thumbnails/27.jpg)
Rails action Path method HTTP verb URL
index bars_path GET /bars
show bar_path(42) GET /bars/42
new new_bar_path GET /bars/new
create bars_path POST /bars
edit edit_bar_path(42) GET /bars/42/edit
update bar_path(42) PUT /bars/42
destroy bar_path(42) DELETE /bars/42
![Page 28: AITP Ruby And Rails Talk](https://reader030.fdocuments.us/reader030/viewer/2022020207/54b94f694a7959702e8b470b/html5/thumbnails/28.jpg)
HAML
![Page 29: AITP Ruby And Rails Talk](https://reader030.fdocuments.us/reader030/viewer/2022020207/54b94f694a7959702e8b470b/html5/thumbnails/29.jpg)
# RHTML version<?xml version="1.0" encoding="utf-8" ?><!DOCTYPE html ...>
<html> <head> <title>Page Title</title> </head> <body> <h1>Page Header</h1> <div id="content"> <p class="news"> Some news content </p> </div> </body></html>
![Page 30: AITP Ruby And Rails Talk](https://reader030.fdocuments.us/reader030/viewer/2022020207/54b94f694a7959702e8b470b/html5/thumbnails/30.jpg)
# HAML version!!! XML!!!
%html %head %title Page Title
%body %h1 Page Header #content %p.news Some news content
![Page 31: AITP Ruby And Rails Talk](https://reader030.fdocuments.us/reader030/viewer/2022020207/54b94f694a7959702e8b470b/html5/thumbnails/31.jpg)
ActiveRecord
![Page 32: AITP Ruby And Rails Talk](https://reader030.fdocuments.us/reader030/viewer/2022020207/54b94f694a7959702e8b470b/html5/thumbnails/32.jpg)
migrations
![Page 33: AITP Ruby And Rails Talk](https://reader030.fdocuments.us/reader030/viewer/2022020207/54b94f694a7959702e8b470b/html5/thumbnails/33.jpg)
class CreateUsers < ActiveRecord::Migration def self.up create_table 'users' do |t| t.belongs_to :area t.string :login, :email, :name t.string :crypted_password, :salt t.timestamps end end
def self.down drop_table 'users' endend
![Page 34: AITP Ruby And Rails Talk](https://reader030.fdocuments.us/reader030/viewer/2022020207/54b94f694a7959702e8b470b/html5/thumbnails/34.jpg)
associations
![Page 35: AITP Ruby And Rails Talk](https://reader030.fdocuments.us/reader030/viewer/2022020207/54b94f694a7959702e8b470b/html5/thumbnails/35.jpg)
class Team < ActiveRecord::Base has_many :playersend
class Player < ActiveRecord::Base belongs_to :teamend
gamecocks = Team.find_by_name("Gamecocks")gamecocks.players # => returns player array
![Page 36: AITP Ruby And Rails Talk](https://reader030.fdocuments.us/reader030/viewer/2022020207/54b94f694a7959702e8b470b/html5/thumbnails/36.jpg)
has many through
![Page 37: AITP Ruby And Rails Talk](https://reader030.fdocuments.us/reader030/viewer/2022020207/54b94f694a7959702e8b470b/html5/thumbnails/37.jpg)
class Reader < ActiveRecord::Base has_many :feeds, :through => :subscriptionsend
class Feed < ActiveRecord::Base has_many :readers, :through => :subscriptionsend
class Subscription < ActiveRecord::Base belongs_to :reader belongs_to :feedend
ars_technica = Feed.find(42)ars_technica.readers # => some large integer
![Page 38: AITP Ruby And Rails Talk](https://reader030.fdocuments.us/reader030/viewer/2022020207/54b94f694a7959702e8b470b/html5/thumbnails/38.jpg)
polymorphic models
![Page 39: AITP Ruby And Rails Talk](https://reader030.fdocuments.us/reader030/viewer/2022020207/54b94f694a7959702e8b470b/html5/thumbnails/39.jpg)
class Client < ActiveRecord::Base has_many :addreses, :as => :addressableend
class Provider < ActiveRecord::Base has_many :addresses, :as => :addressableend
class Address < ActiveRecord::Base belongs_to :addressable, :polymorphic => trueend
Client.first.addresses.build Address.newProvider.first.addresses.build Address.new
![Page 40: AITP Ruby And Rails Talk](https://reader030.fdocuments.us/reader030/viewer/2022020207/54b94f694a7959702e8b470b/html5/thumbnails/40.jpg)
# migrationcreate_table :addresses do |t| t.string :street, :city, :zip_code t.references :addressable, :polymorphic => true # ^^ creates addressable_id # and addressable_typeend
![Page 41: AITP Ruby And Rails Talk](https://reader030.fdocuments.us/reader030/viewer/2022020207/54b94f694a7959702e8b470b/html5/thumbnails/41.jpg)
plugins
![Page 42: AITP Ruby And Rails Talk](https://reader030.fdocuments.us/reader030/viewer/2022020207/54b94f694a7959702e8b470b/html5/thumbnails/42.jpg)
restful_authenticationhttp://github.com/technoweenie/restful-authentication/tree/master
http://railscasts.com/episodes/67
![Page 43: AITP Ruby And Rails Talk](https://reader030.fdocuments.us/reader030/viewer/2022020207/54b94f694a7959702e8b470b/html5/thumbnails/43.jpg)
./script/generate authenticated user sessions
![Page 44: AITP Ruby And Rails Talk](https://reader030.fdocuments.us/reader030/viewer/2022020207/54b94f694a7959702e8b470b/html5/thumbnails/44.jpg)
ym4r-gmhttp://github.com/bitbckt/ym4r-gm/tree/master
![Page 45: AITP Ruby And Rails Talk](https://reader030.fdocuments.us/reader030/viewer/2022020207/54b94f694a7959702e8b470b/html5/thumbnails/45.jpg)
easy google maps integration
![Page 46: AITP Ruby And Rails Talk](https://reader030.fdocuments.us/reader030/viewer/2022020207/54b94f694a7959702e8b470b/html5/thumbnails/46.jpg)
# controller def index @map = GMap.new "map_div" @map.control_init :large_map => true, :map_type => true @map.center_zoom_init [75.5, -42.56], 4
marker = GMarker.new [75.6, -42.467], :title => "title", :info_window => "bar" @map.overlay_init marker end
![Page 47: AITP Ruby And Rails Talk](https://reader030.fdocuments.us/reader030/viewer/2022020207/54b94f694a7959702e8b470b/html5/thumbnails/47.jpg)
# view<html> <head> <%= GMap.header %> <%= @map.to_html %> </head>
<body> <%= @map.div %> </body></html>
![Page 48: AITP Ruby And Rails Talk](https://reader030.fdocuments.us/reader030/viewer/2022020207/54b94f694a7959702e8b470b/html5/thumbnails/48.jpg)
attachment_fuhttp://github.com/technoweenie/attachment_fu/tree/master
![Page 49: AITP Ruby And Rails Talk](https://reader030.fdocuments.us/reader030/viewer/2022020207/54b94f694a7959702e8b470b/html5/thumbnails/49.jpg)
has_attachment :storage => :s3
![Page 50: AITP Ruby And Rails Talk](https://reader030.fdocuments.us/reader030/viewer/2022020207/54b94f694a7959702e8b470b/html5/thumbnails/50.jpg)
SC Ruby ConferenceOctober 18th, 2008
Columbia, SC