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.
- Introduction
- Part 1: Getting our feet wet
- Part 2: Slowly getting there
- Part 3: I/O, I/O, it's off to work we go
- Part 4: Cleaning up our mess
- Part 5: The extra mile
Part 4: Cleaning up our mess
Here is the SongEntry class I promised you in the previous section:
class SongEntry attr_accessor :title, :length def initialize(title, length) @title=title @length=length end def to_s "#{@title} (#{@length})" end end
This class is little more than a wrapper for a string, number pair, but it is chock full of goodies. First, you will notice the attr_accessor thing. If you use C++, you are probably used to writing silly getter and setter functions for each of your private variables. If you use C# or Visual Basic, you are no doubt familiar with the get and set methods that allow you to abstract away the fact that you're calling a function to set a variable. Well, if you want the full power of a function you will have to define one yourself, but if you just want the variables to be accessible from the outside, you can use attr_accessor. These variables are class variables (the @ is implied here). If you want them to be read-only, you can use attr_reader.
Next, note the initialize function. This is the constructor. It takes two parameters, but you can also define parameterless constructor. And while we're on the topic of special functions, to_s is the equivalent of Java's ToString method. It is automatically invoked when Ruby needs to convert something to human-readable form.
Inside to_s, I say
"#{@title} (#{@length})"
The #{ } construct is how you access variables from inside strings. This is equivalent to
@title + ' ' + @length
Both the " " and ' ' characters delimit strings. The difference is that escape sequences are not processed inside of ' ', so stuff like the above won't work. On the other hand, Ruby doesn't have to peer about inside the string for stuff to do, so if you have a string literal with no variables or escape sequences, use ' '.
Putting it all together
Ruby doesn't have a main function or anything like that...it works the same as PHP. Classes and functions are processed but not executed; it's only top-level code that is run. So to run your Ruby program, you need some code that's not in any class (some people will argue that this code is in the global class. Whatever, the end result is the same). Don't worry, I'll give you this code too.
p = Playlist.new p.read('My Playlist.m3u') p.each{|x| puts x}
Note that I'm following my own instructions by putting my string literal in ' ' characters.
So now you have a program that will read and print your playlist. Yeah, not exceptionally useful, I admit, but I hope you learned a bit of Ruby in the process. If you want to stick around, in the next section I talk about ways to make this more useful.