That Stupid Thing I Wrote The Other Day, part 1

| | Comments (1)

Hopefully this will become a series that will let me clean out my Work/misc dir, but "That Stupid Thing I Wrote The Other Day" needs to be changed because TSTIWTOD is a lame acronym. Suggestions?

I use this to populate my iPod with all my music so I don't get confused in music stores:

music = Hash.new { |h,k| h[k] = [] }

Dir.chdir File.expand_path("~/Music/iTunes/iTunes Music") do
  Dir["*"].sort.each do |artist|
    artist_name = artist.sub(/^The (.*)/, '\1, The')
    Dir.chdir artist do
      Dir["*"].sort.each do |album|
        music[artist_name] << album
      end
    end
  end
end

dir = "/Volumes/NanoSpider/Notes/"
dir = test(?d, dir) ? dir : Dir.pwd
out = $stdout
last = nil

music.sort_by { |k,v| k.downcase }.each do |artist, albums|
  letter = artist[0..0].downcase
  if last != letter and dir then
    out.close
    out = File.open("#{dir}/music-#{letter}.txt", "w")
    last = letter
  end
  out.puts "#{artist}:"
  out.puts "  #{albums.sort.join("\n  ")}"
end

out.close if dir

1 Comments

I love learning from others' Ruby code. It would help even more if you could mention ... how you get confused, and how does this script help? Maybe 2 sentences on exactly what it does? many thanks in any case. >TT<

Leave a comment