为了用户方便和更简洁的代码,我想编写一个可以像这样使用的类:
Encoder::Theora.encode do
infile = "path/to/infile"
outfile = "path/to/outfile"
passes = 2
# ... more params
end
现在的挑战是,让这些参数在我的编码方法中可用。
module Encoder
class Theora
def self.encode(&proc)
proc.call
# do some fancy encoding stuff here
# using the parameters from the proc
end
end
end
这种方法行不通。调用 Proc 时,不会在 Theora 类的上下文中评估变量。通常我想使用 method_missing 将每个参数放入 Theora 类的类变量中,但我没有找到正确的输入方式。
谁能指出我正确的方向?