34b01f5a98983c70e7f6163014b94477

This works, but I feel like there should be a more idiomatic way to do this. Forgive me, my ruby-fu is weak.

def archive_files(source, glob_mask, destination)
	Dir.glob(File.join(source, glob_mask)) do |file| 
		should_copy_file = true
		if (block_given?) then should_copy_file = yield(file) end

		if (should_copy_file) then 
			FileUtils.cp_r(file, destination) if File.file?(file) 
		end
	end
end

Refactorings

No refactoring yet !

60888d58de3bf08cbc0c73e67fd6fd86

Josh N

March 12, 2009, March 12, 2009 20:11, permalink

No rating. Login to rate!
def archive_files(source, mask, destination)
  Dir.glob(File.join(source, mask)) do |file| 
    next unless File.file?(file)

    FileUtils.cp(file, destination) if (!block_given? || yield(file)) 
  end
end
60888d58de3bf08cbc0c73e67fd6fd86

Josh N

March 12, 2009, March 12, 2009 20:13, permalink

1 rating. Login to rate!

if i understand correctly you only care about copying files (not folders)

def archive_files(source, mask, destination)
  Dir.glob(source, mask) do |file| 
    next unless File.file?(file)

    FileUtils.cp(file, destination) if (!block_given? || yield(file)) 
  end
end
F1e3ab214a976a39cfd713bc93deb10f

Tj Holowaychuk

March 14, 2009, March 14, 2009 21:41, permalink

No rating. Login to rate!
FileUtils.cp_r Dir['src/*.foo'], '/some/path/'
2ddbd54c304df255bdb709f2cd50ffe8

teamco-anthill.blogspot.com

March 15, 2009, March 15, 2009 12:31, permalink

No rating. Login to rate!

Refactored by Netbeans hints

def archive_files(source, glob_mask, destination)
  Dir.glob(File.join(source, glob_mask)) do |file| 
    should_copy_file = true
    should_copy_file = yield(file) if (block_given?)
    FileUtils.cp_r(file, destination) if File.file?(file) if (should_copy_file)
  end
end
F1e3ab214a976a39cfd713bc93deb10f

Tj Holowaychuk

March 15, 2009, March 15, 2009 17:43, permalink

No rating. Login to rate!

Whats with all these parens you guys? ... and IMO source should just be part of the glob, if not then the glob should have a default and be the last param. also no clue what the yielding of the filepath is about

def archive_files glob, destination, &block
  Dir[glob] do |file| 
    FileUtils.cp_r file, destination if File.file? file
  end
end
F1e3ab214a976a39cfd713bc93deb10f

Tj Holowaychuk

March 15, 2009, March 15, 2009 17:44, permalink

No rating. Login to rate!

whoops get rid of &block

Your refactoring





Format Copy from initial code

or Cancel