新手应该注意哪些 Ruby 陷阱? [英] What are the Ruby Gotchas a newbie should be warned about?
问题描述
我最近学习了 Ruby 编程语言,总而言之,它是一门很好的语言.但是我很惊讶地发现它并不像我想象的那么简单.更准确地说,我似乎不太尊重最小惊喜规则"(当然这是非常主观的).例如:
I have recently learned the Ruby programming language, and all in all it is a good language. But I was quite surprised to see that it was not as simple as I had expected. More precisely, the "rule of least-surprise" did not seem very respected to me (of course this is quite subjective). For example:
x = true and false
puts x # displays true!
和著名的:
puts "zero is true!" if 0 # zero is true!
您会警告 Ruby 新手的其他陷阱"是什么?
What are the other "Gotchas" you would warn a Ruby newbie about?
推荐答案
来自文章:
- 以大写字母开头的名称被视为常量,因此局部变量应以小写字母开头.
- 字符
$
和@
不像 Perl 那样指示变量数据类型,而是用作作用域解析运算符. - 要表示浮点数,必须后跟零位 (
99.0
) 或显式转换 (99.to_f
).附加一个点 (99.
) 是不够的,因为数字容易受到方法语法的影响. - 对非布尔数据的布尔求值是严格的:
0
、""
和[]
都被求值为true代码>.在 C 中,表达式
0 ?1 : 0
计算结果为0
(即假).然而,在 Ruby 中,它产生1
,因为所有数字的计算结果都是true
;只有nil
和false
评估为false
.这个规则的一个推论是,Ruby 方法按照约定——例如,正则表达式搜索——在成功时返回数字、字符串、列表或其他非假值,但在失败时返回nil
(例如,不匹配).这个约定也用于 Smalltalk,其中只有特殊对象true
和false
可以用于布尔表达式. - 1.9 之前的版本缺少字符数据类型(与 C 相比,C 为字符提供类型
char
).这在对字符串进行切片时可能会引起意外:"abc"[0]
产生97
(一个整数,表示字符串中第一个字符的 ASCII 码);获取"a"
使用"abc"[0,1]
(长度为 1 的子串)或"abc"[0].chr
>. 符号
statement until expression
,不同于其他语言的等价语句(例如do { statement } while (not(expression));
in C/C++/...),如果表达式已经是true
,则实际上永远不会运行该语句.这是因为statement until expression
实际上是语法糖
- Names which begin with a capital letter are treated as constants, so local variables should begin with a lowercase letter.
- The characters
$
and@
do not indicate variable data type as in Perl, but rather function as scope resolution operators. - To denote floating point numbers, one must follow with a zero digit (
99.0
) or an explicit conversion (99.to_f
). It is insufficient to append a dot (99.
), because numbers are susceptible to method syntax. - Boolean evaluation of non-boolean data is strict:
0
,""
and[]
are all evaluated totrue
. In C, the expression0 ? 1 : 0
evaluates to0
(i.e. false). In Ruby, however, it yields1
, as all numbers evaluate totrue
; onlynil
andfalse
evaluate tofalse
. A corollary to this rule is that Ruby methods by convention — for example, regular-expression searches — return numbers, strings, lists, or other non-false values on success, butnil
on failure (e.g., mismatch). This convention is also used in Smalltalk, where only the special objectstrue
andfalse
can be used in a boolean expression. - Versions prior to 1.9 lack a character data type (compare to C, which provides type
char
for characters). This may cause surprises when slicing strings:"abc"[0]
yields97
(an integer, representing the ASCII code of the first character in the string); to obtain"a"
use"abc"[0,1]
(a substring of length 1) or"abc"[0].chr
. The notation
statement until expression
, unlike other languages' equivalent statements (e.g.do { statement } while (not(expression));
in C/C++/...), actually never runs the statement if the expression is alreadytrue
. This is becausestatement until expression
is actually syntactic sugar over
until expression
statement
end
,在C/C++中相当于while (not(expression)) statement;
就像statement if expression
等价于
, the equivalent of which in C/C++ is while (not(expression)) statement;
just like statement if expression
is an equivalent to
if expression
statement
end
但是,符号
begin
statement
end until expression
在 Ruby 中,即使表达式已经为真,该语句实际上也会运行一次.
in Ruby will in fact run the statement once even if the expression is already true.
一些与其他语言明显不同的功能:
Some features which differ notably from other languages:
条件表达式的常用运算符,
and
和or
,不遵循正常的优先规则:and
不绑定比或
更紧密.Ruby 还具有表达式运算符||
和&&
,它们按预期工作.
The usual operators for conditional expressions,
and
andor
, do not follow the normal rules of precedence:and
does not bind tighter thanor
. Ruby also has expression operators||
and&&
which work as expected.
def
里面的 def
并没有像 Python 程序员所期望的那样:
def
inside def
doesn't do what a Python programmer might expect:
def a_method
x = 7
def print_x; puts x end
print_x
end
这给出了一个关于 x
未被定义的错误.您需要使用 Proc
.
This gives an error about x
not being defined. You need to use a Proc
.
语言特性
- 如果方法采用多个参数,则省略方法参数周围的括号可能会导致意外结果.Ruby 开发人员表示,在未来的 Ruby 版本中可能不允许省略多参数方法的括号;当前(2007 年 11 月)Ruby 解释器抛出一个警告,鼓励作者不要省略
()
,以避免代码含义模糊.不使用()
仍然是常见的做法,将 Ruby 用作人类可读的特定领域编程语言本身以及称为method_missing()
的方法会特别好.
- Omission of parentheses around method arguments may lead to unexpected results if the methods take multiple parameters. The Ruby developers have stated that omission of parentheses on multi-parameter methods may be disallowed in future Ruby versions; the current (November 2007) Ruby interpreter throws a warning which encourages the writer not to omit
()
, to avoid ambiguous meaning of code. Not using()
is still common practice, and can be especially nice to use Ruby as a human readable domain-specific programming language itself, along with the method calledmethod_missing()
.
这篇关于新手应该注意哪些 Ruby 陷阱?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!