-
-
Save jeffreybaird/2845856 to your computer and use it in GitHub Desktop.
| Dir.chdir #add your music directory here | |
| all_music = (Dir['**/*.mp3'].shuffle) | |
| puts "what do you want to name your playlist?" | |
| answer = gets.chomp | |
| File.open "#{answer}.m3u",'w' do |file| | |
| all_music.each do |mp3_file_name| | |
| file.write "#{mp3_file_name}\n" | |
| end | |
| end | |
| puts "Wrote #{all_music.size} entries" |
gstark
commented
May 31, 2012
- I'd prefer both the directory of music and the output filename be command line options to the script rather than being hard coded and an stdout/stdin situation. This will follow the "unix style" of app writing.
- If you are going to output "Done!" how about "Wrote #{all_amazon_music.size} entries" ?
- "x" is a terrible name for a block variable. How about "file" or "playlist"
- Why are you shuffling the all_amazon_music twice?
- "mp" is also a bad block variable name, how about "mp3_file_name"
- x.write mp+"\n" could be: x.puts mp or x.write "#{mp}\n" Generally good not to get in the habit of building strings with + if you can avoid it. Its not going to be a terrible effect on your app, but in general performance is better with string interpolation than concat.
Awesome! Thanks. I'll have some of the changes up soon.
- I'd be interested in hearing various opinions on using Dir.chdir to set the working directory versus prepending the desired path of the source MP3s and the output path of the m3u file (if they should be different) -- Typically I've hesitated to use Dir.chdir given that it feels like setting a "global state" and I'd rather my code not depend on a certain working directory being set.
The exercise in the book was working on how to create saved outputs.
Would you write it by asking for the directory to navigate to?
I tend to write most of these utilities by supplying the options on the command line rather than having "Where do you want X?" prompts and input capture in the app. It follows the "unix model" where you supply options on the command line and only output data to STDOUT that would be useful output to the user or to another program that would consume it (via pipes)
For example:
randomize_amazon_playlist /path/to/mp3folder /path/to/output_playlist.m3u
Or better yet to use a command line parser:
randomize_amazon_playlist --playlist /path/to/output_playlist.m3u -- mp3_folder /path/to/mp3folder
- Notice I changed the order because with the
--optionstyle you are now free from order dependency.