Filed Under (Ruby) by manatarms on 10-10-2007
Reposted from Rails Weenie:
Install FasterCSV (‘gem install fasterCSV’)
The code to use inside the migration is similar to:
1
2
3
4
| 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)
end |
Filed Under (Ruby) by manatarms on 08-10-2007
I recently needed to recursively compare 2 directories by filename. Here’s the solution:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
| require 'ftools'
srcDir = "/home/Music/"
destDir = "/home/Music/iTunes/iTunes Music"
mHash = Hash.new
iHash = Hash.new
#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] = path
end
puts "Source # Files: " + mHash.length.to_sputs "Dest # Files: " + iHash.length.to_s
#Compare Hashes and copy Any data
Hash.each_key do |key|
if iHash[key] == nil
puts "Copying " + mHash[key] +" to " + destDir
File.copy(mHash[key],destDir+"/")
end
end |