PHP is in widespread use for web applications, but if you want to useRuby on Rails or just want a language that’s more tailored for generaluse, Ruby is worth a look.
Similarities
As in PHP, in Ruby…
- Ruby is dynamically typed, like in PHP, so you don’t need to worryabout having to declare variables.
- There are classes, and you can control access to them like in PHP 5(
public,protected andprivate). - Some variables start with $, like in PHP (but not all).
- There’s
eval, too. - You can use string interpolation. Instead of doing
"$foo is a $bar",you can do"#{foo} is a #{bar}"—like in PHP, this doesn’t apply forsingle-quoted strings. - There’s heredocs.
- Ruby has exceptions, like PHP 5.
- There’s a fairly large standard library.
- Arrays and hashes work like expected, if you exchange
array() for{ and}:array('a' => 'b') becomes{'a' => 'b'}. true andfalse behave like in PHP, butnull is callednil.
Differences
Unlike in PHP, in Ruby…
- There’s strong typing. You’ll need to call
to_s,to_i etc. toconvert between strings, integers and so on, instead of relying on thelanguage to do it. - Strings, numbers, arrays, hashes, etc. are objects. Instead of callingabs(-1) it’s -1.abs.
- Parentheses are optional in method calls, except to clarify whichparameters go to which method calls.
- The standard library and extensions are organized in modules and classes.
- Reflection is an inherent capability of objects, you don’t need to use
Reflection classes like in PHP 5. - Variables are references.
- There’s no
abstract classes orinterfaces. - Hashes and arrays are not interchangeable.
- Only
false andnil are false:0,array() and"" are all truein conditionals. - Almost everything is a method call, even
raise (throw in PHP).