[英]How do I pass a block and an initial argument to each in Ruby?
I use data-tip
for every HTML element to show a tooltip if it has this property. 如果每个HTML元素都具有此属性,我将使用data-tip
来显示工具提示。
Since 以来
data_tip: "a message for you"
looks much nicer than 看起来比
:"data-tip" => "an other message for rudi"
I convert '_' to '-' wherever I am responsible for that. 我负责的地方都将“ _”转换为“-”。
For my simple navigation gem
menu I found a nice recursive solution: 对于我的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)
This works great, but, what if I want to print out the nesting level? 这很好用,但是,如果我想打印出嵌套级别怎么办? Where do I put the starting '0'? 我应该将起始的“ 0”放在哪里?
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)
What curry does: 咖喱的作用:
A curried proc receives some arguments. 咖喱过程接收一些参数。 If a sufficient number of arguments are supplied, it passes the supplied arguments to the original proc and returns the result. 如果提供了足够数量的参数,它将提供的参数传递给原始proc并返回结果。 Otherwise, returns another curried proc that takes the rest of arguments. 否则,返回另一个使用其余参数的咖喱过程。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.