【发布时间】:2018-01-01 01:17:30
【问题描述】:
目前我正在玩一些元编程的想法,让我先介绍一下:
我可以将class 或module 中的字符串“模板”定义为Constant,然后像这样使用它:
class Test1
ConstString = %(the string in here is %{instring})
end
puts Test1::ConstString % { instring: "test" }
#=> the string in here is test
问题是
一个。基于基准,仅定义一个函数就可以将同样的事情提高 3 倍(基准为 1000000.times):
user system total real
Constant: 1.080000 0.000000 1.080000 ( 1.087187)
Function: 0.350000 0.000000 0.350000 ( 0.346578)
b.由于我想如何使用它们,我想将它们与常规函数分开。
所以我决定创建一个新类来继承 Proc...
并包含 module 以输入百分比/模数语法。
module ProcPercentSyntax
def %(*args)
self.call(*args)
end
end
class TestFromProc < Proc
include ProcPercentSyntax
end
class Test2
ConstString = TestFromProc.new { |x, y| %(this is a string with #{x} and #{y}) }
end
那样的话,我可以这样称呼它!
puts Test2::ConstString % "test", "test2"
但是……
#=> this is a string with test and
令人不安的是没有抛出任何错误。
为了确保这不是另一个问题,我继续这样做:
module ProcNotPercentSyntax
def make(*args)
self.call(*args)
end
end
class TestFromProc < Proc
include ProcNotPercentSyntax
end
puts Test2::ConstString.make "test", "test2"
还有……
#=> this is a string with test and test2
请原谅这个问题的冗长零星性质,我将我的意图总结如下:
为什么在使用
%作为方法名称时,该方法似乎遗漏了第二个给定参数?
【问题讨论】:
-
在这种模棱两可的情况下你不需要括号吗?
-
尝试更改您的代码以使其以这种方式工作:
puts Test2::ConstString % ["test", "test2"]。也许 String 的%方法只需要一个参数。 -
它确实适用于明确传递的
[],但是我想知道为什么.make示例接受隐含的"string", "string"和`%` 不接受,如果有办法让它接受隐含的。 -
ProcPercentSyntax 的
%方法中的p args是什么? -
#=> "test"似乎Test2::ConstString % "test", "test2"在这里被解释为[Test2::ConstString.%("test"), "test2"],在这种情况下,我可能不得不满足于您的数组语法。
标签: ruby syntax metaprogramming