Ruby阻止参数名称(Ruby blocks parameter names)
有谁知道是否有一种方法来访问在红宝石块传递参数的名称?
例如
def do_something() # method uses the names of the parameters passed to the block # in addition to their values # e.g. the strings "i" and "j" end do_something { |i, j| ... }这是我写作的dsl的一个要求,也是一个不寻常的用例。 这可能是像parsetree这样的东西,我只是想知道是否有一个更简单/更贴心的方式。
谢谢
Does anyone know if there is a way to access the names of the parameters passed in ruby blocks?
E.g.
def do_something() # method uses the names of the parameters passed to the block # in addition to their values # e.g. the strings "i" and "j" end do_something { |i, j| ... }It's a requirement for a dsl I'm writing, and quite an unusual use case. This is probably possible with something like parsetree, I just wondered if there was an easier/cheekier way.
Thanks
最满意答案
这实际上是可能的,但仅限于1.9的主干版本:
->(a,b,c) {}.parameters它不会被发布,并且很可能会被包含在Ruby 1.9.2中。
This is actually possible, but only in the trunk version of 1.9:
->(a,b,c) {}.parametersIt is not released though and will most probably be included in Ruby 1.9.2.
更多推荐
发布评论