2016-02-09 64 views
0

这里的阵列迭代是一个数组我必须遍历到:通过散列

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 

我去槽的第一个元素,但我不不知道循环如何继续。

有人想知道为什么我不能遍历它吗?

编辑:问题已解决。 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

什么是'to_rsync'? – mudasobwa

+0

'to_rsync'是一个像我之前展示的数组。 – sotaan

回答

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中发生了什么,但我确定它是所有问题的根源。你正在寻找错误的野兽。

+0

非常感谢兄弟!我在'sbx_sync'里面有一行,它调用了我刚刚注释掉的日志函数。现在起作用了 – sotaan