如果你想找出方法來調用語法的一個特定部分翻譯,你可以嘗試出自己:
class << foo = BasicObject.new
def method_missing(meth, *args)
$>.puts "`foo.#{meth}(#{args.inspect[1...-1]})`"
end
BasicObject.instance_methods.each(&method(:undef_method))
end
print '`foo.(1, 2, 3)` gets translated to '
foo.(1, 2, 3)
# `foo.(1, 2, 3)` gets translated to `foo.call(1, 2, 3)`
print '`not foo` gets translated to '
not foo
# `not foo` gets translated to `foo.!()`
print '`+foo` gets translated to '
+foo
# `+foo` gets translated to `[email protected]()`
print '`~foo` gets translated to '
~foo
# `~foo` gets translated to `foo.~()`
print '`foo[1, 2, 3] = 4` gets translated to '
foo[1, 2, 3] = 4
`foo[1, 2, 3] = 4` gets translated to `foo.[]=(1, 2, 3, 4)`
等等......
正如你所看到的,foo.(bar, baz)
被翻譯成foo.call(bar, baz)
。
看看這個答案:[我可以重寫'。'指向數組的索引?](http://stackoverflow.com/questions/37067744/can-i-override-digit-to-point-at-an-index-for-an-array/37067983 #37067983) –
AlexN
@potashin Ohh ?!它實際上是一個lambda?這很酷:)謝謝。我會嘗試一下。您想將您的評論作爲答案嗎? –
@AlexN ok。謝謝。我會檢查出:) –