![](/img/trans.png)
[英]How do I transform a Ruby block and apply it as an argument to another block?
[英]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.