是否有与 Perl 的 Data::Rmap 等效的 Ruby?
Perl 的 Data::Rmap 允许您在数据结构列表上递归地计算 BLOCK (本地设置 $_ 到每个元素)并返回由此类评估结果组成的列表。 $_ 可用于修改元素。
这对于迭代诸如嵌套哈希或哈希数组的层次结构之类的东西非常有用。
Perl's Data::Rmap allows you to recursively evaluate a BLOCK over a list of data structures (locally setting $_ to each element) and return the list composed of the results of such evaluations. $_ can be used to modify the elements.
This is useful for iterating over things like nested hashes, or hierarchies of arrays of hashes and the like.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我认为 Ruby 的
Enumerable
可以满足您想要的一切。 “...并返回由此类评估结果组成的列表”表示您想要Enumerable#map
。 我的第一次尝试是这样的:Ruby's
Enumerable
does everything you want, I think. "... and return the list composed of the results of such evaluations" indicates you wantEnumerable#map
. My first go would be something like this:如果没有真正研究细节,我不确定您是否需要 Ruby 中的模块。 迭代器和块可以做你想做的事。
Without really looking into details, I'm not sure you need a module for that in Ruby. Iterators and blocks are there to do what you want.