Archive for October, 2007

Import CSV Using Rails Migration

October 10, 2007 5 comments

Reposted from Rails Weenie:

Install FasterCSV (‘gem install fasterCSV’)

The code to use inside the migration is similar to:

FasterCSV.foreach(“#{RAILS_ROOT}/lib/symbols_database/security_list.csv“, :row_sep => “\r”) do |row|
field1,field2,field3 = row
Foo.create(:field1 => field1, :field2 => field2, :field3 => field3)

Categories: Ruby, RubyGems, Uncategorized

Comparing Directories Ruby Style

October 8, 2007 Leave a comment

I recently needed to recursively compare 2 directories by filename. Here’s the solution:

require 'ftools' 
srcDir = "/home/Music/"destDir = "/home/Music/iTunes/iTunes Music"

mHash = Hash.newiHash =
 #Add all files to hash Dir[srcDir + '/**/*.mp3'].each do |path|    mHash[path.split('/').last] = pathend

#Add all files to second hash Dir[destDir + "/**/*.mp3"].each do |path|    iHash[path.split('/').last] = pathend

puts "Source # Files: " + mHash.length.to_sputs "Dest # Files: " + iHash.length.to_s

#Compare Hashes and copy Any datam Hash.each_key do |key|     if iHash[key] == nil         puts "Copying " + mHash[key] +" to " + destDir         File.copy(mHash[key],destDir+"/")         end end
Categories: Ruby