Arrays 遍历散列数组
以下是我必须遍历的数组:Arrays 遍历散列数组,arrays,ruby,Arrays,Ruby,以下是我必须遍历的数组: to_rsync = [{"src"=>"medical/", "target"=>"212000/App/", "exclude"=>[".git", "nbproject", ".gitignore"]}, {"src"=>"medical/", "target"=>"44/App/", "exclude"=>[".git", "nbproject", ".gitignore"]}, {"src"=>"m
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
def sbx_sync(from,to,options={})
#展开删除尾部斜杠
#由于ruby 1.8.7的限制,无法使用str[-1]
from=展开(from)+(from.end_随?('/')?'/'):“”)
to=展开(to)+(to.end_与?('/')?'/'):“”)
#默认选项
选项={:archive=>true,:update=>true}.merge(选项)
ops=[]
ops在这里,我去掉了所有不相关的东西,这些东西是多余的,在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\u sync
中发生了什么,但我很确定这是您所有问题的根源。你找错人了。在这里,我去掉了所有不相关的东西,这些东西是多余的,没有在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\u sync
中发生了什么,但我很确定这是您所有问题的根源。你找错人了。to\u rsync
是我刚才展示的数组。to\u rsync
是我刚才展示的数组。非常感谢兄弟!我在sbx\u sync
中有一行代码调用了我刚刚注释掉的日志函数。现在可以了,非常感谢兄弟!我在sbx\u sync
中有一行代码调用了我刚刚注释掉的日志函数。它现在起作用了