Rails for the PHP Dev
(You know youʼre curious...)




November 7, 2009
BIGLAMPCAMP
blog.adsdevshop.com

rdempsey

atlanticdominionsolutions
My Pet Peeves
My Pet Peeves

People that drive slow (in general)
My Pet Peeves

People that drive slow (in general)
People that donʼt use their turn signal
My Pet Peeves

People that drive slow (in general)
People that donʼt use their turn signal
People that read books while they drive
My Pet Peeves

People that drive slow (in general)
People that donʼt use their turn signal
People that read books while they drive
People that canʼt make up their mind
My Pet Peeves

People that drive slow (in general)
People that donʼt use their turn signal
People that read books while they drive
People that canʼt make up their mind
People that post anonymous comments (weak)
My Pet Peeves

People that drive slow (in general)
People that donʼt use their turn signal
People that read books while they drive
People that canʼt make up their mind
People that post anonymous comments (weak)
Trolls, Internet or otherwise
My Pet Peeves

People that drive slow (in general)
People that donʼt use their turn signal
People that read books while they drive
People that canʼt make up their mind
People that post anonymous comments (weak)
Trolls, Internet or otherwise
People that fuck with my sprint
My Pet Peeves

People that drive slow (in general)
People that donʼt use their turn signal
People that read books while they drive
People that canʼt make up their mind
People that post anonymous comments (weak)
Trolls, Internet or otherwise
People that fuck with my sprint
Asshole bosses who wonʼt understand their people
Ruby - History
Ruby - History

Released in 1995 by Yukihiro “Matz” Matsumoto
Ruby - History

Released in 1995 by Yukihiro “Matz” Matsumoto
Blends Perl, Smalltalk, Eiffel, Ada, and Lisp
Ruby - History

Released in 1995 by Yukihiro “Matz” Matsumoto
Blends Perl, Smalltalk, Eiffel, Ada, and Lisp
Natural language syntax
Ruby - History

Released in 1995 by Yukihiro “Matz” Matsumoto
Blends Perl, Smalltalk, Eiffel, Ada, and Lisp
Natural language syntax
Mass acceptance in 2006
Ruby - Objects
Ruby - Objects

Everything is an object
Ruby - Objects

Everything is an object
Everything can be given its own properties (instance
variables) and actions (methods)
Ruby - Objects

Everything is an object
Everything can be given its own properties (instance
variables) and actions (methods)
All types have methods and instance variables
Ruby - Objects

Everything is an object
Everything can be given its own properties (instance
variables) and actions (methods)
All types have methods and instance variables
The same rules apply everywhere
Ruby - Objects

Everything is an object
Everything can be given its own properties (instance
variables) and actions (methods)
All types have methods and instance variables
The same rules apply everywhere
Remove, redefine, and extend at will
Ruby on Rails - History
Ruby on Rails - History

Released in 2005 by David Heinemeier Hansson
Ruby on Rails - History

Released in 2005 by David Heinemeier Hansson
Full-stack framework for developing database-backed web
apps
Ruby on Rails - History

Released in 2005 by David Heinemeier Hansson
Full-stack framework for developing database-backed web
apps
MVC
Ruby on Rails - History

Released in 2005 by David Heinemeier Hansson
Full-stack framework for developing database-backed web
apps
MVC
Builds on the power of Ruby
Ruby on Rails - History

Released in 2005 by David Heinemeier Hansson
Full-stack framework for developing database-backed web
apps
MVC
Builds on the power of Ruby
Convention over configuration
Ruby on Rails - Common Uses

Dynamic web sites
Data warehousing and data mining
Statistics reporting
Management applications
Collaborative apps
Community sites
E-commerce
...
PHP MVC Frameworks

Cake PHP
SolarPHP
Symfony
(others)
Arrays - PHP
Arrays - PHP


in_array($needle, $haystack);
Arrays - PHP


in_array($needle, $haystack);

array_push($haystack, $needle);
Arrays - Ruby


                1
                2
                3
                4
Variable Assignment - PHP
Variable Assignment - PHP


$a = array();
Variable Assignment - PHP


$a = array();

$b = $a;
Variable Assignment - PHP


$a = array();

$b = $a;

$a[ʻfooʼ] = ʻbarʼ;
Variable Assignment - PHP


$a = array();

$b = $a;

$a[ʻfooʼ] = ʻbarʼ;

var_export($a); =>   array(ʻfooʼ => ʻbarʼ)
Variable Assignment - PHP


$a = array();

$b = $a;

$a[ʻfooʼ] = ʻbarʼ;

var_export($a); =>   array(ʻfooʼ => ʻbarʼ)

var_export($b); =>   array()
Variable Assignment - Ruby
Variable Assignment - Ruby


a = {}            => {}
Variable Assignment - Ruby


a = {}            => {}

b=a               => {}
Variable Assignment - Ruby


a = {}               => {}

b=a                  => {}

a[ʻfooʼ] = ʻbarʻ   => “bar”
Variable Assignment - Ruby


a = {}               => {}

b=a                  => {}

a[ʻfooʼ] = ʻbarʻ   => “bar”

a                    => {“foo” => “bar”}
Variable Assignment - Ruby


a = {}               => {}

b=a                  => {}

a[ʻfooʼ] = ʻbarʻ   => “bar”

a                    => {“foo” => “bar”}

b                    => {“foo” => “bar”}
Variable Assignment (2) - Ruby
Variable Assignment (2) - Ruby


a = ʻfooʻ        => “foo”
Variable Assignment (2) - Ruby


a = ʻfooʻ        => “foo”

b=a                => “foo”
Variable Assignment (2) - Ruby


a = ʻfooʻ        => “foo”

b=a                => “foo”

a.reverse!        => “oof”
Variable Assignment (2) - Ruby


a = ʻfooʻ        => “foo”

b=a                => “foo”

a.reverse!        => “oof”

b.reverse!        => “foo”
Variable Assignment (2) - Ruby


a = ʻfooʻ        => “foo”

b=a                => “foo”

a.reverse!        => “oof”

b.reverse!        => “foo”

a.equal? b         => true
http://www.flickr.com/photos/floridapfe/
Methods - PHP


class Monkey

    public function toss($fruit) {

        // toss a piece of fruit at someone

    }

}
Methods - Ruby


class Monkey

 def toss(fruit)

  # toss some fruit at someone

 end

end
Methods (2) - PHP


class Monkey

    public function toss($fruit, $feces = true) {

        // toss a piece of fruit at someone

    }

}
Methods (2) - Ruby


class Monkey

 def toss(fruit, feces = true)

  # toss some fruit at someone

 end

end
Passing Named Params - PHP


class Building {

    public function __construct($height = 10, $width = 10,

                                    $depth = 10)

    {

        // check and store params

    }

}
Passing Named Params (2) - PHP


$b = new Building(10,20,30);
Passing Named Params (3) - PHP


$b = new Building(array(ʻheightʼ => 10,

                         ʻwidthʼ => 20,

                         ʻdepthʼ => 50));
Passing Named Params - Ruby


$b = Building.new(:height => 10,

                  :width => 20,

                  :depth => 50)
Example PHP Function


for ($bananas = 50, $bananas > 0; $bananas --) {

    echo “The monkey has thrown $bananas bananasn”;

}
Example Ruby Block


50.downto(1) { | num | puts “#{num} bananas thrown” }
Example Ruby Block - Hidden Goodies


int.downto(limit) { |i| block } => int



Iterates block, passing decreasing values from int down to
and including limit.
Example Ruby Block


50.downto(1) { |num| puts “#{num} bananas thrown” }
Example Ruby Block - Result


50 bananas thrown

49 bananas thrown

48 bananas thrown

47 bananas thrown

46 bananas thrown

...

1 bananas thrown
Attributes - PHP


class Monkey {
  protected $species;
  public $name;
  public function __construct($species, $name) {
    $this -> species = $species;
    $this -> name = $name;
  }
}
Attributes - Ruby


class Monkey
 def initialize(species, name)
   @species = species
   @name = name
 end

 def species
   @species
 end
 ...
end
Attributes - Ruby


class Monkey
 attr_reader :species
 attr_accessor :name
 attr_writer :bananas_eaten

 def initialize(species, name)
 ...
end
Method Visibility - PHP


public function ...
protected function ...
protected function ...
private function ...
public function ...
Method Visibility - Ruby


class SomeClass

 def my_public_method
 end

 protected

   # Everything down here is protected
Method Visibility - Ruby


class Monkey

 private

   def format(value)
    value.capitalize
   end
Typing- Ruby
Typing- Ruby


foo = ʻbarʻ   =>   “bar”
Typing- Ruby


foo = ʻbarʻ   =>    “bar”

foo = 42       =>     42
Typing- Ruby


foo = ʻbarʻ   =>    “bar”

foo = 42       =>     42

foo + 2        =>      44
Typing- Ruby


foo = ʻbarʻ      =>    “bar”

foo = 42          =>     42

foo + 2           =>      44

foo + “2”.to_i    =>     44
Rails Application Structure


README
Rakefile
app
config
db
doc
lib
log
public
script
test
tmp
vendor
Rails Application Structure - app


app
 - controllers
 - helpers
 - models
 - views
Rails Application Structure - config


config
  - boot.rb
  - database.yml
  - environment.rb
  - environments
  - initializers
  - locales
  - routes.rb
Rails Application Structure - db


db
  - migrations
  - schema.rb
Rails Application Structure - The Rest


doc
lib
log
public
script
test
tmp
vendor
Start a Rails App


rails -d mysql railsforphpdevs
Start a Rails App


rails -d postgres railsforphpdevs
Start a Rails App


rails -d sqlite railsforphpdevs
Thanks!
Questions?

Rails for PHP Developers

Editor's Notes

  • #3 I’m the CEO and Founder of ADS, an Orlando based company that builds custom web and iPhone applications, and helps software companies implement Agile practices
  • #4 We have three apps that we develop
  • #5 Discount code for an additional free month
  • #6 We help connect freelancers and small business people with each other
  • #7 You can find us all over the Internet
  • #8 A little about me I cuss, a lot - if easily offended, Denis Leary: get a fucking helmet What about you? Who here is a PHP developer? Who here has messed around with Rails?
  • #9 A little about me I cuss, a lot - if easily offended, Denis Leary: get a fucking helmet What about you? Who here is a PHP developer? Who here has messed around with Rails?
  • #10 A little about me I cuss, a lot - if easily offended, Denis Leary: get a fucking helmet What about you? Who here is a PHP developer? Who here has messed around with Rails?
  • #11 A little about me I cuss, a lot - if easily offended, Denis Leary: get a fucking helmet What about you? Who here is a PHP developer? Who here has messed around with Rails?
  • #12 A little about me I cuss, a lot - if easily offended, Denis Leary: get a fucking helmet What about you? Who here is a PHP developer? Who here has messed around with Rails?
  • #13 A little about me I cuss, a lot - if easily offended, Denis Leary: get a fucking helmet What about you? Who here is a PHP developer? Who here has messed around with Rails?
  • #14 A little about me I cuss, a lot - if easily offended, Denis Leary: get a fucking helmet What about you? Who here is a PHP developer? Who here has messed around with Rails?
  • #15 A little about me I cuss, a lot - if easily offended, Denis Leary: get a fucking helmet What about you? Who here is a PHP developer? Who here has messed around with Rails?
  • #16 This talk is based off the information presented in Rails for PHP Developers by Derek DeVries and Mike Naberezny I’ll be giving away a copy at the end of this talk
  • #17 I’m not here to convert you. This is not a this or that choice.
  • #18 As developers we have many tools to choose from. Rails is another tool we can use to build web apps.
  • #19 IMO, it’s easiest to learn Rails coming from PHP In fact, I was a php developer before moving to Rails The good news: there are a lot of similarities, which we’ll see To be a good Rails programmer, you need to know Ruby
  • #24 Highly flexible, which means that you can easily fuck it up
  • #25 Highly flexible, which means that you can easily fuck it up
  • #26 Highly flexible, which means that you can easily fuck it up
  • #27 Highly flexible, which means that you can easily fuck it up
  • #28 Highly flexible, which means that you can easily fuck it up
  • #34 Let’s dig into the specifics
  • #35 MVC like Rails
  • #36 So without further ado, let’s dig in.
  • #37 So without further ado, let’s dig in.
  • #38 in_array(value, array) array_push(array, value)
  • #39 in_array(value, array) array_push(array, value)
  • #40 Going back to the idea that everything in Ruby is an object, here’s an example.
  • #41 $a has the one value, and $b is empty All assignments here are by value
  • #42 $a has the one value, and $b is empty All assignments here are by value
  • #43 $a has the one value, and $b is empty All assignments here are by value
  • #44 $a has the one value, and $b is empty All assignments here are by value
  • #45 $a has the one value, and $b is empty All assignments here are by value
  • #46 In Ruby, both a and b contain a reference to the same Hash object In Ruby, the assignment always assigns by reference - big difference
  • #47 In Ruby, both a and b contain a reference to the same Hash object In Ruby, the assignment always assigns by reference - big difference
  • #48 In Ruby, both a and b contain a reference to the same Hash object In Ruby, the assignment always assigns by reference - big difference
  • #49 In Ruby, both a and b contain a reference to the same Hash object In Ruby, the assignment always assigns by reference - big difference
  • #50 In Ruby, both a and b contain a reference to the same Hash object In Ruby, the assignment always assigns by reference - big difference
  • #51 Methods on a and b are acting on the same object
  • #52 Methods on a and b are acting on the same object
  • #53 Methods on a and b are acting on the same object
  • #54 Methods on a and b are acting on the same object
  • #55 Methods on a and b are acting on the same object
  • #56 To help us look at methods, we turn to our friend the monkey
  • #57 Some are small, some look really cool. But all monkeys throw things.
  • #58 Pass in a single param
  • #59 Param (fruit) is optional
  • #60 Multiple params, set a default value You might get the fruit, but you have 100% chance of getting the feces
  • #61 Basic method declaration and optional params are pretty much the same
  • #62 Pass in our params and give them default values
  • #63 Use the class we created and create a building Need to ensure we have the right order for the params Hell if I’m going to remember that
  • #64 Using named params
  • #65 Using named params Here we use symbols, indicated by the “:”
  • #66 In Ruby we have blocks, which are like closures in javascript PHP 5.3 supports this
  • #67 Here we see a monkey throwing bananas by using a for loop As long as we have bananas to throw, the monkey throws them
  • #68 50 is an integer object, and we’re calling the downto method
  • #69 50 is an integer object, and we’re calling the downto method
  • #70 A block can take optional params, in this case the banana count, put between the goal posts The rest of the block is executable code
  • #71 The result What’s cool - every method in Ruby has the capability of being passed a block
  • #72 PHP uses data members to share data Visibility depends on the keywords used in the declaration
  • #73 The @species is similar to the PHP code No need for getter methods!
  • #74 attr_reader replaces our getter method attr_accessor replaces both the getter and setter attr_writer adds a setter method w/out the corresponding getter
  • #75 We have a mix of public, private, and protected based on the declaration
  • #76 Unless below the protected line, everything is public Protected methods cannot be publicly called
  • #77 Private methods in Ruby can be executed only within the context of the same object or derivative objects
  • #78 We can start with a string Retype on the fly Check that that’s happened
  • #79 We can start with a string Retype on the fly Check that that’s happened
  • #80 We can start with a string Retype on the fly Check that that’s happened
  • #81 We can start with a string Retype on the fly Check that that’s happened
  • #82 Quick overview of the Rails app structure and creating a Rails app
  • #83 README and Rakefile are files, the rest are directories
  • #84 app is where the bulk of the code lives
  • #85 All of the config files Supported databases: sqlite, postgres, mysql, oracle, MS SQL, more Environments: development, test, production Routes - all the REST goes here
  • #86 All migration files stored here (timestamped) Can roll forward or backward (and all data will be fubar) Schema.rb - current database schema
  • #90 Sqlite is default if you don’t pick the database you want Supports - MySQL, Postgres, Sqlite, MS SQL, Oracle, and many others