原文如下:
http://www.ruby-lang.org/zh_CN/documentation/ruby-from-other-languages/to-ruby-from-python/
Python is another very nice general purpose programming language. Going from Python to Ruby, you’ll find that there’s a little bit more syntax to 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 instead ofpydoc
). - There are no special line terminators (except the usual newline).
- String literals can span multiple lines like Python’s triple-quoted strings.
- Brackets are for lists, and braces are for dicts (which, in Ruby, are called “hashes”).
- Arrays work the same (adding them makes one long array, but composing them 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 the same.
- You’ve got embedded doc tools (Ruby’s is called rdoc).
Differences
Unlike Python, in Ruby,...
- Strings are mutable.
- You can make constants (variables whose value you don’t intend to change).
- There are some enforced case-conventions (ex. class names start with a capital 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 the results of Ruby expressions directly into other strings without having to
"add " + "strings " + "together"
). Single-quoted strings are like Python’sr"raw strings"
. - There are no “new style” and “old style” classes. Just one kind.
- You never directly access attributes. With Ruby, it’s all method calls.
- Parentheses for method calls are usually optional.
- There’s
public
,private
, andprotected
to enforce access, instead of Python’s_voluntary_
underscore__convention__
. - “mixin’s” are used instead of multiple inheritance.
- You can re-open a class anytime and add more methods.
- You’ve got
true
andfalse
instead ofTrue
andFalse
(andnil
instead ofNone
). - When tested for truth, only
false
andnil
evaluate to a false value. Everything else is true (including0
,0.0
,""
, and[]
). - It’s
elsif
instead ofelif
. - It’s
require
instead ofimport
. Otherwise though, usage is the same. - The usual-style comments on the line(s) above things (instead of docstrings below them) are used for generating docs.
- There are a number of shortcuts that, although give you more to remember, you quickly learn. They tend to make Ruby fun and very productive
翻译转自博客园 http://www.cnblogs.com/gpcuster/archive/2008/11/24/1339679.html
Python是一门非常优秀的语言,从Python迁移到Ruby,你将发现一些语法上的不同。
相似点
和Python一样,在Ruby中,...
- 有一个交互提示 (叫做 irb).
- 你可以在命令行中读取文档 (通过ri 命令来替代 pydoc).
- 没有特殊的结束一行的符号 (新行除外).
- 文字可以用多行,就像Python中的三个引号.
- List用[],Dict用{} (Dict在Ruby中叫“hashes”).
- Arrays的工作方式相同(2个Array相加成为一个更加长的Array,但是想这样a3 = [ a1, a2 ] 合并,将产生一个包含数组的数组).
- Objects是有固定类型和动态转换的.
- 一切都是object,变量名只是一个指向Object的指针.
- 虽然关键字不同, 但是exceptions的工作方式不变.
- 你拥有嵌入文档的工具 (在Ruby中叫做rdoc).
不同点
与Python不同, 在Ruby中,...
- Strings是可以改变的。
- 你可以使用常量(常量的值是不同改变的)。
- 这里有些强制规定的书写要求 (例如:class的名字以大写字母开头, 变量名已小写字母开头)。
- 这里只有一种容器(Array), 并且是可以改变的。
- 引号中的String限制不同。
- 这里没有新风格的Class,Class只有一种风格。
- 你无法直接访问属性,在Ruby中,都是通过方法调用实现的。
- 在方法调用中使用(),是一种可选的策略。
- 这里用private等等限制访问的关键字,来替代Python中的名字隐藏。
- “mixin’s”用来替代多继承。
- 你可以在任何时候修改已有的Class,并添加新的方法。
- 用true和false来替代True和False (用nil来替代None)。
- 在判断真值的时候,只有false和nil会被认为是假.其他所有的都认为是真(包括0, 0.0, "",和[])。
- 用elsif替代elif.
- 用require替代import. 但是用法是相同的。
- 用usual-style来注释文档(替代docstrings) 同时用来生成文档。
最新评论