在你看来,你遇到过的最令人惊讶、最怪异、最奇怪或最“WTF”的语言特性是什么?
请每个回答只回答一个特征。
在你看来,你遇到过的最令人惊讶、最怪异、最奇怪或最“WTF”的语言特性是什么?
请每个回答只回答一个特征。
当前回答
红宝石拖鞋。条件语句中的"…"和".."并不总是范围操作符:
(0..20).each do |x|
if ((x%10) == 5)..((x%10) == 5)
print "#{x} "
end
end
(0..20).each do |x|
if ((x%10) == 5)...((x%10) == 5)
print "#{x} "
end
end
这将输出:
5 15
5 6 7 8 9 10 11 12 13 14 15
. .检查每一遍的两个语句,…每次只检查“on”或“off”语句(取决于触发器状态)。它们是从awk和sed偷来的。
Matz在“Ruby编程语言”中写道:“人字拖是Ruby中一个相当晦涩的特性,可能最好避免……”
其他回答
Python的everything-is-really-a-reference有一个有趣的副作用:
>>> a = [[1]] * 7
>>> a
[[1], [1], [1], [1], [1], [1], [1]]
>>> a[0][0] = 2
>>> a
[[2], [2], [2], [2], [2], [2], [2]]
我一直是PHP错误的忠实粉丝,当在一行中使用两个冒号时脱离上下文:
解析错误:语法错误,第3行/path/to/file/error.php中的T_PAAMAYIM_NEKUDOTAYIM异常
第一次遇到这种情况时,我完全被弄糊涂了。
在javaScript中,NaN是一个全局变量。
在Javascript中,我认为以下是等价的:
a['title'] = "Syntactic sugar is good for yr teeth.";
a.title = "Syntactic sugar is good for yr teeth.";
在Java中从文本文件中读取一行。
BufferedReader in = null;
try {
in = new BufferedReader(new FileReader("filename"));
String str;
str = in.readLine();
if (str != null) {
...
}
} catch (IOException e) {
...
} finally {
try {
if (in != null) {
in.close();
}
} catch (IOException e) {}
}
啊。虽然我承认这并不奇怪……只是邪恶。: -)
更短、更习惯的版本:
try {
BufferedReader in = new BufferedReader(new FileReader("filename"));
try {
String str = in.readLine();
while (str != null) {
str = in.readLine();
}
} finally {
in.close();
}
} catch (IOException e) {
e.printStackTrace();
}