Servage Magazine

Information about YOUR hosting company – where we give you a clear picture of what we think and do!

Getting started with Ruby

Monday, May 19th, 2014 by Servage

ruby-or-php

One of the questions people often ask about Rails is how it differs from PHP. PHP is a general-purpose scripting language that can be embedded right into HTML pages, making it easy for developers to create dynamically generated web pages quickly and easily. Many web designers and most web developers have used PHP in some capacity. Because of its proliferation (it’s usually installed by default on most webhosts), PHP is often the go-to language for handling simple tasks like keeping your website’s navigation current, randomizing images on a website, and even creating a simple content management system. PHP is also useful for creating full-blown open source and commercial web applications such as WordPress, and HelpSpot, both PHP applications.

Ruby Vs PHP

Technically speaking, we shouldn’t compare PHP, a programming language, to Rails, a web application framework. Instead, we should compare PHP to the Ruby programming language upon which Rails was built. Ruby was created in 1995 by Yukihiro “Matz” Matsumoto, and has slowly built a following, exploding in popularity and getting more mainstream attention in 2006, in no small part because of the popularity of Rails. At the time of this writing, Ruby ranks as the 9th most popular programming language in the world.

To quote the Ruby website, “Ruby is a dynamic, open source programming language with a focus on simplicity and productivity. It has an elegant syntax that is natural to read and easy to write.” This elegance and focus on simplicity makes Ruby a very approachable language for people new to programming, and offers a refreshing change of pace for seasoned developers.

Let’s run through a quick example, just to get a “feel” for what we’re talking about. Let’s say we want to count from 1 to 10. In PHP, the code might look like this:

for ($i = 1; $i <= 10; $i++) {
  echo $i;
}

If you’re familiar with C, Java, or other C-like control structures, this is straightforward for you. But if you’re new to this, that might seem pretty intimidating.  Here’s the same code in Ruby:

10.times do |i|
  puts i
end

Ruby also has its own tricks and shortcuts that would make even a serious Perl hacker happy. The following line does the very same thing as the examples above:

puts *(1..10)

This kind of flexibility and simplicity makes Ruby an approachable language for software developers and web designers of all levels. While PHP is a web-friendly programming language, Rails is a web application framework written in Ruby (and with access to all of Ruby’s functionality to boot). And because of the way Rails works, each application you build lives in the form of a project, with specific files and folders. Unlike many PHP apps which often “just work” when uploaded to a webserver, Rails apps rely on their framework and a customized hosting infrastructure (often called a “stack”). As a result, Rails applications can be a bit more challenging to deploy. Fortunately, a number of web hosting companies specialize in deploying, hosting, and managing Rails applications. So how would you know when to use Rails and when to use PHP? There isn’t anything that one could do in Rails that they couldn’t do in PHP (or vice versa), so in the end it boils down to a matter of choice. For me personally, I have a simple rule: if I’m adding simple functionality (such as rotating header images) to an otherwise straightforward website, I’ll often use PHP. If I’m building a web application, especially one with a database, I’ll use Rails. Again, both could do either, but I find the Rails framework is wonderful for the kind of web application development I like to do. It should be noted that there are frameworks with goals similar to those of Ruby on Rails that are written in PHP, such as CakePHP and CodeIgniter.

 Sources for further reading

Getting started with Ruby, 3.0 out of 5 based on 2 ratings
Categories: Guides & Tutorials

Keywords: ,

You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.

No comments yet (leave a comment)

You are welcome to initiate a conversation about this blog entry.

Leave a comment

You must be logged in to post a comment.