繁体   English   中英

如何在Ruby中将一个块和一个初始参数传递给每个?

[英]How do I pass a block and an initial argument to each in Ruby?

如果每个HTML元素都具有此属性,我将使用data-tip来显示工具提示。

以来

data_tip: "a message for you"

看起来比

:"data-tip" => "an other message for rudi"

我负责的地方都将“ _”转换为“-”。

对于我的simple navigation gem菜单,我找到了一个不错的递归解决方案:

cleanup=Proc.new do |item|
  cleanup_hash item.html_options #<- this does the '_' -> '-'
  item.sub_navigation.items.each(&cleanup) if item.sub_navigation
end
navigation.primary_navigation.items.each(&cleanup)

这很好用,但是,如果我想打印出嵌套级别怎么办? 我应该将起始的“ 0”放在哪里?

你可以用curry

cleanup=Proc.new do |depth=0, item|
  cleanup_hash item.html_options #<- this does the '_' -> '-'
  item.sub_navigation.items.each(&cleanup.curry[depth + 1]) if item.sub_navigation
end
navigation.primary_navigation.items.each(&cleanup)

咖喱的作用:

咖喱过程接收一些参数。 如果提供了足够数量的参数,它将提供的参数传递给原始proc并返回结果。 否则,返回另一个使用其余参数的咖喱过程。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM