如果每个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)
咖喱做什么:
curried proc接收一些参数。如果
提供了参数,它将提供的参数传递给
原始过程并返回结果否则,返回另一个
接受其余参数的curried proc。
关于ruby - 如何在Ruby中将一个块和一个初始参数传递给每个?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/24764937/