|
| 1 | +--- |
| 2 | +layout: page |
| 3 | +title: To Ruby From Python |
| 4 | +--- |
| 5 | + |
| 6 | +Python is another very nice general purpose programming language. Going |
| 7 | +from Python to Ruby, you’ll find that there’s a little bit more syntax |
| 8 | +to learn than with Python. |
| 9 | + |
| 10 | +### Similarities |
| 11 | + |
| 12 | +As with Python, in Ruby,... |
| 13 | + |
| 14 | +* There’s an interactive prompt (called `irb`). |
| 15 | +* You can read docs on the command line (with the `ri` command instead |
| 16 | + of `pydoc`). |
| 17 | +* There are no special line terminators (except the usual newline). |
| 18 | +* String literals can span multiple lines like Python’s triple-quoted |
| 19 | + strings. |
| 20 | +* Brackets are for lists, and braces are for dicts (which, in Ruby, are |
| 21 | + called “hashes”). |
| 22 | +* Arrays work the same (adding them makes one long array, but composing |
| 23 | + them like this `a3 = [ a1, a2 ]` gives you an array of arrays). |
| 24 | +* Objects are strongly and dynamically typed. |
| 25 | +* Everything is an object, and variables are just references to objects. |
| 26 | +* Although the keywords are a bit different, exceptions work about the |
| 27 | + same. |
| 28 | +* You’ve got embedded doc tools (Ruby’s is called rdoc). |
| 29 | + |
| 30 | +### Differences |
| 31 | + |
| 32 | +Unlike Python, in Ruby,... |
| 33 | + |
| 34 | +* Strings are mutable. |
| 35 | +* You can make constants (variables whose value you don’t intend to |
| 36 | + change). |
| 37 | +* There are some enforced case-conventions (ex. class names start with a |
| 38 | + capital letter, variables start with a lowercase letter). |
| 39 | +* There’s only one kind of list container (an Array), and it’s mutable. |
| 40 | +* Double-quoted strings allow escape sequences (like \\t) and a special |
| 41 | + “expression substitution” syntax (which allows you to insert the |
| 42 | + results of Ruby expressions directly into other strings without having |
| 43 | + to `"add " + "strings " + "together"`). Single-quoted strings are like |
| 44 | + Python’s `r"raw strings"`. |
| 45 | +* There are no “new style” and “old style” classes. Just one kind. |
| 46 | +* You never directly access attributes. With Ruby, it’s all method |
| 47 | + calls. |
| 48 | +* Parentheses for method calls are usually optional. |
| 49 | +* There’s `public`, `private`, and `protected` to enforce access, |
| 50 | + instead of Python’s `_voluntary_` underscore `__convention__`. |
| 51 | +* “mixin’s” are used instead of multiple inheritance. |
| 52 | +* You can add or modify the methods of built-in classes. Both languages |
| 53 | + let you open up and modify classes at any point, but Python prevents |
| 54 | + modification of built-ins — Ruby does not. |
| 55 | +* You’ve got `true` and `false` instead of `True` and `False` (and `nil` |
| 56 | + instead of `None`). |
| 57 | +* When tested for truth, only `false` and `nil` evaluate to a false |
| 58 | + value. Everything else is true (including `0`, `0.0`, `""`, and `[]`). |
| 59 | +* It’s `elsif` instead of `elif`. |
| 60 | +* It’s `require` instead of `import`. Otherwise though, usage is the |
| 61 | + same. |
| 62 | +* The usual-style comments on the line(s) *above* things (instead of |
| 63 | + docstrings below them) are used for generating docs. |
| 64 | +* There are a number of shortcuts that, although give you more to |
| 65 | + remember, you quickly learn. They tend to make Ruby fun and very |
| 66 | + productive. |
| 67 | +* There’s no way to unset a variable once set (like Python’s `del` |
| 68 | + statement). You can reset a variable to `nil`, allowing the old |
| 69 | + contents to be garbage collected, but the variable will remain in the |
| 70 | + symbol table as long as it is in scope. |
0 commit comments