Skip to content

Instantly share code, notes, and snippets.

@ejallday
Last active December 15, 2015 18:59
Show Gist options
  • Select an option

  • Save ejallday/5307842 to your computer and use it in GitHub Desktop.

Select an option

Save ejallday/5307842 to your computer and use it in GitHub Desktop.
Building an Mp3 Playlist.
# The hardest part about this exercise for me was figuring out how to get the filenames
# from my music folder into an array that could be fed into this method. I had never used
# the Dir[] class before so it was a bit foreign to me to think that I could just give Dir
# a file path and a parameter to collect file extensions and they would all just be in an
# array. Good to know.
def shuffle array
array = array.shuffle!
filename = 'Erics_playlist.m3u'
File.open filename, 'w' do |f|
f.puts array
end
end
tracks = Dir['/Users/User/Desktop/Music/**/*.{mp3,Mp3,MP3}']
shuffle tracks
# Step 1: define my method and give it an argument of array
# Step 2: shuffle the array permanently with #shuffle! of the array class
# Step 3: Tell the program what I want my playlist name to be
# Step 4: Using File.open filename, we create the file with 'w' write privileges
# and tell the file to puts the array. This is key for arrays. puts must be used
# rather than print or write.
# In the future, I would extend this method to a class so that I could use instance variables with gets.chomp
# and ask the user what they would like to call the playlist, and ask what the file path of the songs they would
# like to be put in a playlist are. Also, It would be nice to add the functionality of only selecting a specific
# number of songs from a file or selecting all of them and then popping off a bunch of them so that the user can
# determine how many songs they want in the list. That way if a folder has 100 songs, but you only want a playlist
# featuring 5 random songs, you could do it.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment