Ruby阻止参数名称(Ruby blocks parameter names)

系统教程 行业动态 更新时间:2024-06-14 16:59:46
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) {}.parameters

It is not released though and will most probably be included in Ruby 1.9.2.

更多推荐

本文发布于:2023-04-17 08:46:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/dzcp/e33f9f8fdbc36083bb15a54497ee4fe0.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:名称   参数   Ruby   names   parameter

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!