在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下摆,我只需要关心任务的“相关”部分,而不是始终浏览所有的样板文件。