sotaan
sotaan

Reputation: 135

Iterating through an array of hash

Here is an array I have to traverse to:

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"]}]

I looped over it like this:

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

I went trough the first element, but I don't know how the loop doesn't continue.

Is anybody have an idea of why I can't traverse it?

Edit: Problem solved. sbx_sync stopped the execution. Here is the code to help you understand:

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

The problem was in my log function that I commented out.

Upvotes: 0

Views: 47

Answers (1)

Aleksei Matiushkin
Aleksei Matiushkin

Reputation: 121000

Here I got rid of all unrelated stuff, which is redundant and not described in the 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/", ...}

As you can see, the array is iterated pretty fine.

I have no clue, though, what is happening in sbx_sync, but I am pretty sure it is the source of all your problems. You are hunting the wrong beast.

Upvotes: 2

Related Questions