Perl is awesome. Perl’s docs are awesome. The Perl community is … awesome. For those Perlers who long for elegant OO features built-in from the beginning, Ruby may be for you.
Similarities
As with Perl, in Ruby,…
- You’ve got a package management system, somewhat like CPAN (though it’s called RubyGems).
- Regexes are built right in. Bon appétit!
- There’s a fairly large number of commonly-used built-ins.
- Parentheses are often optional.
- Strings work basically the same.
- There’s a general delimited string and regex quoting syntax similar to
Perl’s. It looks like
%q{this}(single-quoted), or%Q{this}(double-quoted), and%w{this for a single-quoted list of words}. You%Q|can|%Q(use)%Q^other^delimiters if you like. - You’ve got double-quotish variable interpolation, though it
"looks #{like} this"(and you can put any Ruby code you like inside that#{}). - Shell command expansion uses
`backticks`. - You’ve got embedded doc tools (Ruby’s is called rdoc).
Differences
Unlike Perl, in Ruby,…
- You don’t have the context-dependent rules like with Perl.
- A variable isn’t the same as the object to which it refers. Instead, it’s always just a reference to an object.
- Although
$and@are used as the first character in variable names sometimes, rather than indicating type, they indicate scope ($for globals,@for object instance, and@@for class attributes). - Array literals go in brackets instead of parentheses.
- Composing lists of other lists does not flatten them into one big list. Instead you get an array of arrays.
- It’s
definstead ofsub. - There’s no semicolons needed at the end of each line. Incidentally,
you end things like function definitions, class definitions, and case
statements with the
endkeyword. - Objects are strongly typed. You’ll be manually calling
foo.to_i,foo.to_s, etc., if you need to convert between types. - There’s no
eq,ne,lt,gt,ge, norle. - There’s no diamond operator (
<>). You usually useIO.some_methodinstead. - The fat comma
=>is only used for hash literals. - There’s no
undef. In Ruby you havenil.nilis an object (like anything else in Ruby). It’s not the same as an undefined variable. It evaluates tofalseif you treat it like a boolean. - When tested for truth, only
falseandnilevaluate to a false value. Everything else is true (including0,0.0, and"0"). - There’s no PerlMonks. Though the ruby-talk mailing list is a very helpful place.