What is Toluu?
Toluu is a free service for sharing the feeds you read and discovering new ones.
Get Invite

Rails for PHP Developers


Empty VariablesSeptember 23

One of the more confusing differences between Ruby and PHP is the different way in which the languages treat empty variables when evaluating statements.

The chart below shows the most common scenarios you may encounter. We have several similar charts in the Rails for PHP Developers book with other handy information.

PHP if ($x) empty($x)   $x = ""; FALSE TRUE   $x = null; FALSE TRUE   $x = array(); FALSE TRUE   $x = false; FALSE TRUE   $x = 0; FALSE TRUE   $x = "0"; FALSE TRUE     Ruby if (x) x.empty? x.blank?** x = "" TRUE TRUE TRUE x = nil FALSE u/m * TRUE x = [] TRUE TRUE TRUE x = {} TRUE TRUE TRUE x = false FALSE u/m * TRUE x = 0 TRUE u/m * FALSE x = "0" TRUE FALSE FALSE
* NoMethodError: undefined method ** Only Available in Rails

While PHP evaluates all empty variable as false, Ruby only evaluates nil and false to be false. Some Ruby objects have an empty? method, but this is not always as useful as you might expect. The method is only available on some objects, and calling nil.empty? or false.empty? will result in NoMethodError.

The Rails framework adds a consistent method named blank? that exists on all objects as a single way to check if a variable contains a blank object. Rails has also very recently added a corresponding present? method, which is equival

Type Casting and ConversionsSeptember 17

In the Rails for PHP Developers book we mention that Ruby objects have conversion methods to translate between different object types.

Object Conversion Methods

Methods such as to_s and to_i are very commonly used to translate between different objects.

PHP Ruby convert to integer (int) "3"; "3".to_i convert to float (float) "1.2"; "1.2".to_f convert to string (string) 1; 1.to_s convert to array (array) "test"; [*"test"]

Kernel Module Conversion Methods

This isn’t the only way to convert object types in Ruby. We can use Kernel module conversion methods as well. These methods include Array, Float, Integer, and String.

PHP Ruby convert to integer (int) "3"; Integer("3") convert to float (float) "1.2"; Float("1.2") convert to string (string) 1; String(1) convert to array (array) "test"; Array("test")

The Differences

These methods perform similar actions to the instance conversion methods, but have some subtle differences.

Integer will honor base indicators such as (0, 0x, and 0b), but String#to_i will not. If Integer cannot convert an object it will throw an ArgumentError.

Ruby

"0x12".to_i # => 0 Integer("0x12") # => 18   "a".to_i # => 0 Integer
Applying Ruby’s BlocksSeptember 9

Not long ago, we took a look at the basics of Ruby Block Scope. When you’re first getting started with Ruby’s blocks (closures), little things like that can be frustrating. Blocks can seem so foreign that you might be tempted to think that they’ll make your code more difficult to read or understand. Once you get past the learning curve, blocks can be leveraged to improve the readability and maintainability of code in some situations.

Here’s a few examples of applying Ruby’s blocks to everyday problems.

Handling Timeouts

Ruby’s Timeout library is used to ensure that a block of code doesn’t execute longer than a certain amount of time. This is a handy feature since the block can wrap many operations which might not otherwise support a configurable timeout.

Ruby

require 'timeout'   Timeout::timeout(20) do # Potentially long-running code end

The Timeout::timeout method takes a timeout value in seconds. If the block takes longer to execute than the timeout value, it will be interrupted.

Working with Files

In PHP, file_get_contents() and file_put_contents() provide convenient ways to quickly read and writ

Deploying on Phusion PassengerMay 14

A very large number of PHP developers, perhaps even the majority, are building smaller web applications. These applications receive only a moderate amount of traffic and usually have a single database server, often on the same machine.

Deployment, or moving your application to production for use by real customers, is largely an afterthought for these small PHP applications. In many cases, decent PHP code can just be installed on the server and it runs without much trouble.

One of the points we stress towards the back of our book is that deploying Rails applications can be more difficult than deploying their PHP counterparts. There are more moving parts and things to you’ll need to learn. Until you get the hang of it, deploying even small Rails applications can be frustrating.

A recent development has greatly improved this situation.

Introducing Phusion Passenger

PHP can be deployed using a variety of server configurations. Some of these can be just as frustrating as traditional Rails deployments. However, the majority of PHP applications are still deployed on Apache using mod_php. PHP’s tight integration with Apache is simple, proven, and just works for many needs.

Phusion is a small company in the Netherlands that recently released an open source product called

Separating Public FilesMay 9

In chapter 5 of the book, we discuss the importance of separating public files in your web applications. Rails applications have a separate public/ directory where all public assets such as CSS, Javascript, and images are stored. No configuration files or Ruby code is stored in this directory, and we don’t need to worry about server directives being set correctly to secure files our application.

The word public/ is perfectly descriptive, and acts as a cognitive warning when serving content. Anything in the public/ directory is free and open for users to download.

It’s a great idea to use this same idea in PHP applications. PHP should interpret files with certain file extensions (such as files ending in .php), but mistakes happen. When you name the web accessible directory of your applications public/, it is immediately obvious as to the accessibility of the content. This raises a red flag when you’re uploading sensitive documents or information into a publicly accessible directory.

Most of the PHP frameworks follow this idea, and keep their application logic in a directory that is not publicly accessible from the web.