Many visitors have written in to let us know they found our PHP to Ruby Reference useful and have asked how they can contribute. We’re pleased to announce that the source files for the reference are now hosted on GitHub and we’re accepting contributions.
If you haven’t tried Git or GitHub yet, this would be a great excuse. Visit the GitHub Guides for a collection of resources that will help get you started.
Here’s what you’ll need to do to contribute: Create a GitHub accou
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; FA
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"]
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
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.
The word public/ is perfectly descriptive, and acts as a cognitive warning when serving co
If you look up at the navigation bar, you’ll see we’ve added a new Reference section.
When you’re starting out with Ruby and Rails, often times you’ll know how to do something in PHP and need a way to map that knowledge into the Ruby world. For example, you might ask yourself, “What’s the equivalent of fopen in Ruby?”
Now, you can get the answer just like you would on php.net — http://railsforphp.com/fopen
You can also enter a PHP function name into the search box at t
The MD5 and SHA1 cryptographic hashes are commonly used to verify the integrity of files or create a fixed length digital representation of a string or file. PHP has some very easy functions to create hashes of both strings and files.
Like most utility functions, generating these hashes is not part of the core Ruby objects. Instead, the Ruby Standard Library includes the Digest library. Let’s take a look at the equivalents to PHP’s common cryptographic hash functions in the Ruby unive
Ruby’s blocks, or closures, are a feature that does not have a direct equivalent in PHP. We devote a fair number of pages to this topic in the book. Even so, it will take a bit of time and practice before you feel completely comfortable with them. Let’s take a look at an easy way that Ruby’s block scoping might trip you up.
In this example, we have an array of fruit. We want to iterate through the array and print the name of each fruit. At the end, we want to print the name of the las
Rails uses quite a few techniques to make the end-user API a little nicer when you’re creating your applications. One of these techniques is having a method accept variable arguments with a hash of options. We see this pattern appear in quite a few different areas of the Rails API, particularly in ActiveRecord and ActionView.
An an example, we can specify a variable number of attributes when we perform model validation. Take a look at how we validate the presence of both the name and