Some time ago I started using Ruby in my projects. Before that moment I had been mostly coding in PHP. So comparison of these two languages was quite inevitable when I switched to Ruby. On initial stage I used Codeacademy resources to learn more about language potential and its distinctive features. Even now having good command of the Ruby language I still keep comparing Ruby and PHP and come to new conclusions.
In this article I would to make a quick comparative overview of Ruby and PHP based on my initial experience.
Ruby as an object-base language
Despite the fact that both Ruby and PHP are interpreted languages Ruby is fully object-base. It means that all objects in Ruby are eventually derived from Object base class.
Therefore, data type String s = ‘Hello, world in Ruby turns into String object. We can see it by calling method .class which is derived from Object.
Object-base approach lets us make necessary changes quickly. For example, let’s unfold the lines and delete all “o” letters. The first example is in Ruby.
s.reverse.delete(‘o')
PHP
str_replace(‘o','',strrev($s));
As you can see the code does not look good at all in PHP. So it makes a point for Ruby.
If/unless modifiers
Another feature that I noticed was the use of if/unless modifiers.
Ruby
puts ‘Hello world’ if true puts ‘Hello world’ unless false
PHP
if (true) { echo ‘Hello, world’; }
Another point for Ruby in my opinion.
Abstract classes and interfaces
Now let’s compare the implementation of abstract classes and interfaces.
In PHP it looks like this.
interface ICreature { public function walk($steps); public function say($text); }
PHP interpreter immediately detects Fatal Error. So we will have to run these methods.
Ruby, on the contrary, has other approach.
class ICreature def walk(steps, degree) raise ‘Method should be implemented’ end def say(text) raise ‘Method should be implemented’ end end class Human < ICreature end
Ruby interpreter runs the code with no exception alerts so we spot exceptions rather late when we call methods. It is a point for PHP.
Extending classes
Ruby provides an interesting option to extend classes.
For instance, we decide to extend standard Float class by adding method ‘in_miles’. This method is supposed to return distance in miles.
class Float def in_miles self / 1.6 end end
As a result, we are able to do the following
1600.0.in_miles => 1000.0
Operator+ as an instance method
Operator+ is quite common for PHP. However, in Ruby its behaviour is different. Operator+ becomes an instance method. So we are able to define our method + in any class. By the way, method+ is unary. So it can use one parameter only.
class Complex attr_accessor :a, :b def initialize(a,b) self.a = a self.b = b end def + (complex) Complex.new(self.a += complex.a, self.b += complex.b) end def to_s "(#{a}) + i(#{b})" end end c1 = Complex.new(1,3) c2 = Complex.new(-1, -4) (c1+c2).to_s => (0) + i(-1)
Ruby as a new language to master
Ruby is a comparatively new language which attracts more and more developers. They create various libraries and tools distributed as gems. It is very convenient as you can download them and add to projects easily. In addition, there are many large Ruby communities like Rubygems that help to share ideas and interact with other developers.
To sum it up, I would like to say that there is no winner here. The languages are different and they both have good sides and drawbacks. It is just a matter of personal preferences and habits. They are both cool.