Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 56 lines (49 sloc) 2.803 kb
8ed805a @ihower Revert "translate to chinese", it's should keep the original content.
ihower authored
1 Python is another very nice general purpose programming language.
2 Going from Python to Ruby, you'll find that there's a little bit more
3 syntax to learn than with Python.
8768db2 @ihower Init project
ihower authored
4
8ed805a @ihower Revert "translate to chinese", it's should keep the original content.
ihower authored
5 h3. Similarities
8768db2 @ihower Init project
ihower authored
6
8ed805a @ihower Revert "translate to chinese", it's should keep the original content.
ihower authored
7 As with Python, in Ruby,...
8 * There's an interactive prompt (called @irb@).
9 * You can read docs on the command line (with the @ri@ command instead
10 of @pydoc@).
11 * There are no special line terminators (except the usual newline).
12 * String literals can span multiple lines like Python's triple-quoted
13 strings.
14 * Brackets are for lists, and braces are for dicts (which, in Ruby, are called
15 "hashes").
16 * Arrays work the same (adding them makes one long array, but composing
17 them like this @a3 = [ a1, a2 ]@ gives you an array of arrays).
18 * Objects are strongly and dynamically typed.
19 * Everything is an object, and variables are just references to objects.
20 * Although the keywords are a bit different, exceptions work about the same.
21 * You've got embedded doc tools (Ruby's is called rdoc).
8768db2 @ihower Init project
ihower authored
22
23
8ed805a @ihower Revert "translate to chinese", it's should keep the original content.
ihower authored
24 h3. Differences
25
26 Unlike Python, in Ruby,...
27 * Strings are mutable.
28 * You can make constants (variables whose value you don't intend to change).
29 * There are some enforced case-conventions (ex. class names start
30 with a capital letter, variables start with a lowercase letter).
31 * There's only one kind of list container (an Array), and it's mutable.
32 * Double-quoted strings allow escape sequences (like \t) and
33 a special "expression substitution" syntax (which allows you to insert
34 the results of Ruby expressions directly into other strings without
35 having to @"add " + "strings " + "together"@). Single-quoted strings
36 are like Python's @r"raw strings"@.
37 * There are no "new style" and "old style" classes. Just one kind.
38 * You never directly access attributes. With Ruby, it's all method calls.
39 * Parentheses for method calls are usually optional.
40 * There's @public@, @private@, and @protected@ to enforce access, instead
41 of Python's @_voluntary_@ underscore @__convention__@.
42 * "mixin's" are used instead of multiple inheritance.
43 * You can add or modify the methods of built-in classes. Both languages let you open up and modify classes at any point, but Python prevents modification of built-ins — Ruby does not.
44 * You've got @true@ and @false@ instead of @True@ and @False@ (and @nil@
45 instead of @None@).
46 * When tested for truth, only @false@ and @nil@ evaluate to a false value.
47 Everything else is true (including @0@, @0.0@, @""@, and @[]@).
48 * It's @elsif@ instead of @elif@.
49 * It's @require@ instead of @import@. Otherwise though, usage is the same.
50 * The usual-style comments on the line(s) _above_ things (instead of
51 docstrings below them) are used for generating docs.
52 * There are a number of shortcuts that, although give you more to
53 remember, you quickly learn. They tend to make Ruby fun and very
54 productive.
55
Something went wrong with that request. Please try again.