Using Ruby to Process m3u Playlists

Written By: Nathan Baker

- 06 Apr 2006 -
















Description: Programming in Ruby is fun! In this tutorial, we will use Ruby to process the m3u playlists used by a number of media players.

  1. Introduction
  2. Part 1: Getting our feet wet
  3. Part 2: Slowly getting there
  4. Part 3: I/O, I/O, it's off to work we go
  5. Part 4: Cleaning up our mess
  6. Part 5: The extra mile

Introduction

You're a Good Programmer. You learned to write code back when, if you wanted a string, you had to malloc it yourself. You thrust raw bits into the fire, and with strokes of your mighty hammer you forged Order and Discipline from Chaos. Your days were the frontier days of programming, where the men were men and the women, well, they may have been women but with some it was hard to tell.

But now you hear these kids running around and talking about their scripting languages. Yes, Python, Ruby, Perl, PHP, these terms get thrown around like Java exceptions. As you complete your thousand-line C function that will display a single pixel on the screen, you bitterly think to yourself that all this means is that the vast amounts of bad code on the web has become vast amounts of bad, slow code. But you're an open-minded kinda person, and you want to give these kiddy languages a second look, and maybe find out that they're not popular because they're easy (though they are, compared to C), but because they're easy but powerful.

So let's take a short tour of the Ruby language and its environment, then we'll create an actual program. Maybe along the way you will realize that, for once, the whippersnappers were right after all. But don't worry; we won't tell.

First Impressions

I'm assuming here that you have Ruby installed on your system. No matter what heathen operating system you're using, there's probably a port of Ruby for it. And even if you're running something hideously strange, so long as your platform of choice has a port of gcc it can probably build Ruby. Ruby files are plain text, so all you need to edit Ruby programs is a text editor like notepad or nano. There are syntax-highlighting, bracket-matching, auto-indenting fancy-schmancy editors, too, if you're that kind of person.

Creating a document is fairly straightforward. We just instantiate the Document class

Ruby files (generally given the .rb extension, though it doesn't really matter) are invoked by typing "ruby filename.rb" at the command prompt, or (if you're on a UNIX-like system) you can use the familiar #!/bin/ruby at the top of the file. If you're into GUIs, you can also register the Ruby interpreter as the default handler for .rb files. You don't get to see console output this way, though, so do it like the 1970s and bust out that black screen.

The Ruby language itself will cause a great amount of head-scratching among the C/C++/C#/Java/PHP people who are used to languages whose syntax more or less mimics C's. Those who were into Smalltalk, LISP, or other 'alternative lifestyles' back in college will probably see things that look a bit more familiar, as will people who have experience in Perl or Python. Ruby isn't quite as bad as Perl for creating expressions that look like a train wreck, but you can still have a pretty high symbol-to-word ratio if you try hard. You can throw around arbitrary blocks of code (think lambda expressions) willy-nilly, and PCRE (Perl-compatible regular expression) support is built-in: regular expressions are first-class values.

Well, now that we know a bit about the language, let's do something fun with it!

Next >>