PHP to Ruby on Rails Translations
I used to develop primarily in PHP and dabbled into ASP but then I learned about Ruby and the Rails framework and now I cringe when I need to develop something new that isn’t in Ruby. There are a ton of tutorials on Rails so if you aren’t familiar or want to learn just do some googling. My intention with this post is to compare the differences in chunks of common code between php and ruby. Items such as for loops, array manipulation in ruby, various string functions in ruby and php, etc. I obviously won’t write everything so if you think of something that would be handy to know then leave a comment with it in there. Yes I realize PHP has frameworks that add some of the handy features that Rails.
Create Array:
Ruby: myArray = ["dog","cat","mouse"]
PHP: $myArray = array(”dog”,”cat”,”mouse”)
*With Ruby there’s no need to specify that it’s an array, it knows from the brackets. Also notice that PHP uses parentheses and Ruby uses brackets.
Using element of array by position
Ruby: myArray[1] = “cat”
PHP: $myArray(1) = “cat”
First Elements:
Ruby: myArray.first = “dog”
PHP: first($myArray) = “dog”
Ruby: myArray.first(2) = ["dog","cat"]
PHP: for ($i=0; $i < 1; $i++ ){$first2elements .= myArray($i);} * If there is a better way to do this in PHP let me know if it doesn’t involved a loop.
Print out elements of Array
Ruby: myArray.inspect
PHP: printr($myArray)
Comments
Ruby: # COMMENT
*Ruby doesn’t have multiline comments at the moment, just get a good editor that has a shortcut for it such as Textmate or Aptana.
PHP: // SINGLE LINE COMMENT
/* MULTILINE COMMENT */
Code Tags
Ruby: <%=%> is to embed code in a view typically used for items such as the beginning of a loop.
PHP: (Short version), (Long Version)
*Both languages support putting an equal sign after the opening to print out what is in the tag. In Ruby if something isn’t meant to be printed out such as the opening loop tag then it will get mad at you since there’s nothing to print in that portion.
A quick note on Ruby before I go into the string section. You can call quite a few methods on arrays and strings that permanately change the current object you are working on. The Exclamation point is what tells Ruby to keep the changes you made to it or if you leave it off it remains the same when you call it again. For instance you can do…
String Manipulation
Ruby: s = “hello world”
PHP: $s = “hello world”
Ruby: s.capitalize = “Hello World”
PHP: ucwords($s) = “Hello World”
s.capitalize! = “Hello World” *This keeps the string Capitalized
s.capitalize = “Hello World” *The string is capitlized but only for this call on the capitlize method, if you want you can store it in a new variable to keep the capitlization.
Other good things to know about Ruby
Form Validation
Ruby: validates_presence_of :animal_name *This goes in the definition of the model such as ‘animals’, going into detail about how models work is beyond the scope of this tutorial. Other handy ones you can use in rails are…
validates_acceptance_of *Good to use this for checkboxes.
validates_format_of *Can use regular expressions to validate fields such as email address.
validates_length_of :animal_name, :maximum = 20 *A max of 20 characters is aloud
validates_length_of :animal_name, :within= 5..20 *Needs to be within 5 to 20 characters
validates_numericality_of :feet *Makes sure that the user specifies a number for amount of feet
validates_presence_of :animal_name *Make sure animal name isn’t blank
validates_uniqueness_of :animal_name *Make sure you only have one entry for ‘dog’ as an animal name
PHP: There are several PHP frameworks that have different ways of doing similar validation techniques try, CakePHP is a very popular one.
Link Creation
<%= link_to “link name” “/direction/for/link” %>
<%= javascript_include_tag “myJSFile” %>
<%= stylesheet_link_tag “myCSSFile” %>
A great resource for PHP to rails/ruby translations is http://railsforphp.com


