在Ruby中,一個方法可以使用blocks/lambda表達式,並使您可以編寫看起來像是語言一部分的構造。例如,在Fixnum
類times
方法:如何在elisp中定義以「塊」作爲參數的函數?
2.times do
# whatever code is between do and end, will be executed 2 times
end
或者例如,在File
類的open
方法:
File.open(some_file) do |file|
# do something with the file; once the block finishes execution, the handle is automatically closed
end
open方法可能有類似這樣的實現(藉口紅寶石「僞」):
class File
def open(file,mode="r",&blk)
begin
handle = probably_sys_open(file)
if block_given?
blk.call(handle)
close(handle)
else
return handle
end
rescue SomeException => e
# whatever error handling goes on here
end
end
end
我怎麼能寫這樣的功能/ elisp的方法,這樣,當我請使用t下襬,我只需要關心任務的「相關」部分,而不是始終瀏覽所有的樣板文件。