在Ruby中,如何向每个模块传递一个块和一个初始参数?

在Ruby中,如何向每个模块传递一个块和一个初始参数?,ruby,Ruby,我对每个HTML元素使用数据提示,如果它具有此属性,则显示工具提示 自 看起来比以前好多了 :"data-tip" => "an other message for rudi" 无论我在哪里负责,我都会将“u”转换为“-” 对于我的简单导航gem菜单,我找到了一个很好的递归解决方案: cleanup=Proc.new do |item| cleanup_hash item.html_options #<- this does the '_' -> '-' item.s

我对每个HTML元素使用
数据提示
,如果它具有此属性,则显示工具提示

看起来比以前好多了

:"data-tip" => "an other message for rudi"
无论我在哪里负责,我都会将“u”转换为“-”

对于我的
简单导航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)
cleanup=Proc.new do |项|
cleanup_hash item.html_选项#'-'
item.sub_导航.items.each(&cleanup)if item.sub_导航
结束
navigation.primary\u navigation.items.each(&cleanup)
这很好,但是,如果我想打印出嵌套级别呢?起始“0”放在哪里?

您可以使用

cleanup=Proc.new do | depth=0,项目|
cleanup_hash item.html_选项#'-'
item.sub_导航.items.each(&cleanup.curry[depth+1])如果item.sub_导航
结束
navigation.primary\u navigation.items.each(&cleanup)
咖喱的作用是:

curry进程接收一些参数。如果有足够数量的 如果提供了参数,它会将提供的参数传递给 原始进程并返回结果。否则,返回另一个 接受其余参数的curry进程

你可以用

cleanup=Proc.new do | depth=0,项目|
cleanup_hash item.html_选项#'-'
item.sub_导航.items.each(&cleanup.curry[depth+1])如果item.sub_导航
结束
navigation.primary\u navigation.items.each(&cleanup)
咖喱的作用是:

curry进程接收一些参数。如果有足够数量的 如果提供了参数,它会将提供的参数传递给 原始进程并返回结果。否则,返回另一个 接受其余参数的curry进程


咖喱像“盐”、“胡椒”或“辣椒”?(红宝石名称!)。好的,这里需要一个很好的canditate来让代码变得不可读,不是吗?不,像Haskell一样的咖喱。哦,我现在聪明了一点;Lambda演算:-/,我可以在深度理论上睡一两个晚上吗?咖喱?像“盐”、“胡椒”或“辣椒”?(红宝石名称!)。好的,这里需要一个很好的canditate来让代码变得不可读,不是吗?不,像Haskell一样的咖喱。哦,我现在聪明了一点;Lambda演算:-/,我可以睡一两个晚上吗?
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)
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)