在你看来,你遇到过的最令人惊讶、最怪异、最奇怪或最“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中一个相当晦涩的特性,可能最好避免……”
其他回答
在Bash中,变量可以显示为标量和数组:
$ a=3
$ echo $a
3
$ echo ${a[@]} # treat it like an array
3
$ declare -p a # but it's not
declare -- a="3"
$ a[1]=4 # treat it like an array
$ echo $a # acts like it's scalar
3
$ echo ${a[@]} # but it's not
3 4
$ declare -p a
declare -a a='([0]="3" [1]="4")'
$ a=5 # treat it like a scalar
$ echo $a # acts like it's scalar
5
$ echo ${a[@]} # but it's not
5 4
$ declare -p a
declare -a a='([0]="5" [1]="4")'
KSH做同样的事情,但是使用排版而不是声明。
当你在zsh中这样做时,你得到的是子字符串赋值而不是数组:
$ a=3
$ a[2]=4 # zsh is one-indexed by default
$ echo $a
34
$ a[3]=567
$ echo $a
34567
$ a[3]=9
$ echo $a
34967
$ a[3]=123 # here it overwrites the first character, but inserts the others
$ echo $a
3412367
$ a=(1 2 3)
$ echo $a
1 2 3 # it's an array without needing to use ${a[@]} (but it will work)
$ a[2]=99 # what about assignments?
$ echo $a
1 99 3
Python for循环中的else。
来自Python文档:
for n in range(2, 10):
for x in range(2, n):
if n % x == 0:
print n, 'equals', x, '*', n/x
break
else:
# loop fell through without finding a factor
print n, 'is a prime number'
输出:
2 is a prime number
3 is a prime number
4 equals 2 * 2
5 is a prime number
6 equals 2 * 3
7 is a prime number
8 equals 2 * 4
9 equals 3 * 3
我有点纠结:
1;
在perl中,模块需要返回true。
在Coldfusion中,任何地方的变量都被视为全局变量,无论它们放在哪里。
<cffunction name="one" returntype="void">
<cfset var wtf="coldfusion">
<cfinvoke method="second">
</cffunction>
<cffunction name="two" returntype="void">
<cfoutput>#wtf#</cfoutput>
</cffunction>
两个字:多重继承。这毫无意义,只会制造麻烦。
编辑——我指的是c++中的MI,而不是Java和其他语言中的mixins之类的。