迭代哈希数组

时间:2016-02-09 14:27:37

标签: arrays ruby

这是我必须遍历的数组:

to_rsync = [{"src"=>"medical/",
  "target"=>"212000/App/",
  "exclude"=>[".git", "nbproject", ".gitignore"]},
 {"src"=>"medical/",
  "target"=>"44/App/",
  "exclude"=>[".git", "nbproject", ".gitignore"]},
 {"src"=>"medical/",
  "target"=>"trisomie21/App/",
  "exclude"=>[".git", "nbproject", ".gitignore"]},
 {"src"=>"medical/",
  "target"=>"04/App/",
  "exclude"=>[".git", "nbproject", ".gitignore"]},
 {"src"=>"medical/",
  "target"=>"carrosse/App/",
  "exclude"=>[".git", "nbproject", ".gitignore"]},
 {"src"=>"medical/",
  "target"=>"49/App/",
  "exclude"=>[".git", "nbproject", ".gitignore"]},
 {"src"=>"medical/",
  "target"=>"53/App/",
  "exclude"=>[".git", "nbproject", ".gitignore"]},
 {"src"=>"medical/",
  "target"=>"72/App/",
  "exclude"=>[".git", "nbproject", ".gitignore"]},
 {"src"=>"medical/",
  "target"=>"85/App/",
  "exclude"=>[".git", "nbproject", ".gitignore"]}]

我像这样循环:

lab_path = File.expand_path("~#{user}/lab")
webroot_path = File.expand_path("~#{user}/webroot")
Pry::ColorPrinter.pp(to_rsync)
to_rsync.each do |h|
    src = File.join(lab_path, h["src"])
    dst = File.join(webroot_path, h["target"])
    sbx_sync src, dst, {:chown => 'www-data:www-data', :exclude => h["exclude"]}
end

我通过了第一个元素,但我不知道循环是如何继续的。

有人知道我为什么不能穿越它吗?<​​/ p>

编辑:问题解决了。 sbx_sync停止了执行。 以下是帮助您理解的代码:

def sbx_sync(from, to, options = {})
    # expand removes trailing slash
    # cannot use str[-1] due to ruby 1.8.7 restriction
    from = expand(from) + (from.end_with?('/') ? '/' : '')
    to = expand(to) + (to.end_with?('/') ? '/' : '')
    # default options
    options = { :archive => true, :update => true }.merge(options)
    ops = []
    ops << '-a' if options[:archive]
    ops << '-v' if options[:verbose]
    ops << '-u' if options[:update]
    ops << '-m' if options[:prune_empty]
    ops << '-n' if @file_options[:noop]

    Array(options[:exclude]).each do |path|
      ops << "--exclude=#{ sh_escape(path) }"
    end

    ops << "--chown=#{ sh_escape(options[:chown]) }" if options[:chown]
    ops << '--delete' if options[:delete]
    command = "rsync #{ ops.join(' ') } #{ sh_escape(from) } #{ sh_escape(to) } 2>&1"
    puts command
    #stdout = cmd(command)
    #log("Sync from #{ sh_escape(from) } to #{ sh_escape(to) }.  STDOUT:\n\n#{ stdout }")
end

问题出在我注释掉的日志功能中。

1 个答案:

答案 0 :(得分:2)

在这里,我摆脱了所有不相关的东西,这是多余的,并没有在OP中描述:

 ▶ to_rsync = [{"src"=>"medical/",
 ▷     "target"=>"212000/App/",  
 ▷   "exclude"=>[".git", "nbproject", ".gitignore"]},    
 ▷   {"src"=>"medical/", 
 ▷     "target"=>"44/App/",  
 ▷ "exclude"=>[".git", "nbproject", ".gitignore"]}]      
 #⇒ [
 # [0] {
 #   "exclude" => [
 #     [0] ".git",
 #     [1] "nbproject",
 #     [2] ".gitignore"
 #   ],
 #       "src" => "medical/",
 #    "target" => "212000/App/"
 # },
 # [1] {
 #   "exclude" => [
 #     [0] ".git",
 #     [1] "nbproject",
 #     [2] ".gitignore"
 #  ],
 #       "src" => "medical/",
 #    "target" => "44/App/"
 # }
 # ]
 ▶ to_rsync.each { |h| puts h.inspect }
 #⇒ {"src"=>"medical/", "target"=>"212000/App/", ...}
 #⇒ {"src"=>"medical/", "target"=>"44/App/", ...}

如您所见,数组迭代得非常好。

我不知道sbx_sync发生了什么,但我很确定这是你所有问题的根源。你正在寻找错误的野兽。