Python is another very nice general purpose programming language. Goingfrom Python to Ruby, you’ll find that there’s a little bit more syntaxto learn than with Python.
Similarities
As with Python, in Ruby,…
- There’s an interactive prompt (called
irb). - You can read docs on the command line (with the
ri command insteadofpydoc). - There are no special line terminators (except the usual newline).
- String literals can span multiple lines like Python’s triple-quotedstrings.
- Brackets are for lists, and braces are for dicts (which, in Ruby, arecalled “hashes”).
- Arrays work the same (adding them makes one long array, but composingthem like this
a3 = [ a1, a2 ] gives you an array of arrays). - Objects are strongly and dynamically typed.
- Everything is an object, and variables are just references to objects.
- Although the keywords are a bit different, exceptions work about thesame.
- You’ve got embedded doc tools (Ruby’s is called rdoc).
- There is good support for functional programming with first-classfunctions, anonymous functions, and closures.
Differences
Unlike Python, in Ruby,…
- Strings are mutable.
- You can make constants (variables whose value you don’t intend tochange).
- There are some enforced case-conventions (ex. class names start with acapital letter, variables start with a lowercase letter).
- There’s only one kind of list container (an Array), and it’s mutable.
- Double-quoted strings allow escape sequences (like
\t) and a special“expression substitution” syntax (which allows you to insert theresults of Ruby expressions directly into other strings without havingto"add " + "strings " + "together"). Single-quoted strings are likePython’sr"raw strings". - There are no “new style” and “old style” classes. Just one kind.(Python 3+ doesn’t have this issue, but it isn’t fully backwardcompatible with Python 2.)
- You never directly access attributes. With Ruby, it’s all methodcalls.
- Parentheses for method calls are usually optional.
- There’s
public,private, andprotected to enforce access,instead of Python’s_voluntary_ underscore__convention__. - “mixins” are used instead of multiple inheritance.
- You can add or modify the methods of built-in classes. Both languageslet you open up and modify classes at any point, but Python preventsmodification of built-ins — Ruby does not.
- You’ve got
true andfalse instead ofTrue andFalse (andnilinstead ofNone). - When tested for truth, only
false andnil evaluate to a falsevalue. Everything else is true (including0,0.0,"", and[]). - It’s
elsif instead ofelif. - It’s
require instead ofimport. Otherwise though, usage is thesame. - The usual-style comments on the line(s)above things (instead ofdocstrings below them) are used for generating docs.
- There are a number of shortcuts that, although give you more toremember, you quickly learn. They tend to make Ruby fun and veryproductive.
- There’s no way to unset a variable once set (like Python’s
delstatement). You can reset a variable tonil, allowing the oldcontents to be garbage collected, but the variable will remain in thesymbol table as long as it is in scope. - The
yield keyword behaves differently. In Python it will returnexecution to the scope outside the function’s invocation. Externalcode is responsible for resuming the function. In Rubyyield willexecute another function that has been passed as the final argument,then immediately resume. - Python supports just one kind of anonymous functions, lambdas, whileRuby contains blocks, Procs, and lambdas.