Created
May 31, 2012 20:01
-
-
Save jeffreybaird/2845856 to your computer and use it in GitHub Desktop.
Script for creating a playlist Chapter 11 exercises
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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" |
Author
- 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.
Author
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.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Awesome! Thanks. I'll have some of the changes up soon.