I can’t afford to buy a house here, that’s a fact. However internet space is much cheaper so I’ve decided to get my own domain, my own blog and possibly my own code. I’ve been hosting elsewhere for a while, always moving here and there instead of building my own place but that’s not a good long term strategy. It’s time to settle down.

I’m begging you my dear readers: follow me! I promise I’ll write more, I’m actually cooking a few (hopefully) good stuff: more on Apache, ODE and some Javascript and XULRunner goodness.

New address:
New feed:

Thanks and see you there!


Streaming to an Airport Express in Ruby

In the current Apple frenzy, what’s better than a post about an old Apple product: the Airport Express. Small and versatile, this device can be used for several different applications but it’s most popular to send music over wi-fi (to your good ole’ amplifier located on the other side of your apartment for example). It’s also a pretty cheap thing.

I mentioned in my last post that I was using my little reader script to feed an Airport Express and somebody asked me how. First, I have to say that there isn’t much to do, thanks to the nice raop client created by Aaron Patterson. So like many other Ruby hacks, it starts with a:

gem install raop-client

Playing an MP3 file to your Airport is now just a small script and a command away:

require 'raop'
raop ="")
# Closer to 0 is louder
raop.volume=5 $stdin

What looks like an IP address above ( really is an IP address. It’s the one of your Airport Express on your local network, I’m sure you now how to find it (hint: your router probably knows). Save this script in a file (aex.rb for example). It takes a stream of raw wav as input, so we’ll need to summon the power of LAME (Lame Ain’t an MP3 Encoder) to produce the stream:

lame --decode "Joe Dassin - Les Champs-Élysées.mp3" - | ruby aex.rb

Mind the dash after the file name, it’s important. If you’re not a Linux happy user ™, you’ll need to get LAME. If you’re a Windows user, good luck with the pipe. Divide & Conquer with Cygwin would probably work though.

Note that making open source software that uses MP3 files isn’t an easy task. The Fraunhofer Society owns a patent on the MP3 format and has enforced it in several occasions (generating a good deal of revenue). Everything that encodes, decodes or reads MP3s have to pay and yes, that includes your iPod. I don’t think it ever went directly against open source developers but then it never said it wouldn’t either. A very reasonable move would be to authorize GPL licensed software to use their patent. I’m sure they use open source software as well, so they should really stop leaching.

Anyway you’re now happily playing songs to your Airport Express but you’re still missing the part. As I said, raop client accepts any wav stream so it’s just a matter of piping the stream (obtained in my last installment) through LAME and redirecting that to raop client.

Airport Express piping

To glue this with the script I’ve shown you last time, just do:

lastfm = LastFM.login("mriou", "****")
aex ="")

If you want to see the whole script plus a few additional niceties, it’s available on Rubyforge. You can checkout the whole thing and then run bin/lastfm.rb.

svn co svn:// dubya

Happy listening!

Streaming in Ruby

Irecently have been unlucky enough to experience a hard drive crash. The unrecoverable type of crash. Being on the light side for backups, I’ve lost all my (legally obtained of course) collection of mp3. Tons and gigs of them. I’m not crying because I’m a man but I can tell you that my heart bleeds. So as a result, I pretty much rely on internet radios now, among which The trick though, is that I run Linux so their default player (iTunes nastiness) doesn’t work for me. And I hate iTunes anyway. Amarok is much better but I own an Airport Express and I can’t easily stream to my Airport using Amarok. Long story to say that I needed some more creativity.

Turns out that the protocol is pretty simple. A very nice person has retro-engineered it in a pretty thorough manner. So I won’t repeat everything that has already been explained in detail, just sum up and add a couple more missing things:

  • The stream is a plain old MP3 stream (more poms for the Maven fans out there). It doesn’t have any Shoutcast style meta data.
  • On the other hand, they insert a “SYNC” string in there signal anytime the song changes in there stream, to let you know what’s happening.
  • To get what’s currently playing they have a web service (the kind of RESTful type) that can be invoked anytime and returns all kind of information about the song. That with the previous point allow you to know what’s happening. There are also a few more services to skip, love, ban and let them know how you really feel right now.
  • To start the interaction, you only need to login by invoking a URL, sending you username and password. I can hear your knees jerking my security-aware friends, but don’t worry, the password is MD5 encoded. They reply to you with a session id that you pass in any subsequent calls.

I told you it was simple. So I went ahead and implemented the whole thing in Ruby, which turned out to be equally simple, once all the aforementioned details were clear. And here is the thing:

LastFM script

Now what do you do with that little “out” variable? Pretty much everything you want. You really can’t pass a file, to stream everything to your disk and therefore get MP3s for everything you’re listening. That would be illegal. Plus the RIAA people don’t like it and we should really listen to them, they act for the good of music.

Personally, as already mentioned, I stream it to my Airport Express using the Ruby RAOP client that Aaron Patterson has developed. And I can finally enjoy the simplest form of Art ever made.

A Diagram For Your Rails Records

Sometimes, when it’s getting late, after a few beers and when you have more than a couple of Active Records in your project, things can get a bit blurry. What is linked to what? Did I make that a belong_to or a has_many? Clearly, the bigger picture can elude you.

Fortunately, I just discovered (thanks to Matt Biddulph) some way to generate a quick and easy diagram showing how your active records play with each other. But don’t start thinking I needed this because I had a beer. I did not. Or did I? Anyway, it’s fortunately pretty simple.

  1. First you’ll need graphviz. If you’re running the right O/S it’s as straightforward as ‘apt-get install graphviz’. The two of you running Fedora can replace apt-get by yum, I’m guessing it would work just as well. For Windows and Mac users, it’s just a download.
  2. Save the Ruby script reproduced below in a ‘graph.rb’ file located at the root of your Rails project.
  3. Run ‘ruby graph.rb | dot -Tpng -ograph.png’. It won’t work with the wrong O/S (understand Windows). You’ll have to save the output of the script in a file and then run the second part of the command on that file. Also if you’re dorky enough to have some ‘puts’ in your ActiveRecord you’ll have to save the output in a file as well and clean that up.
  4. Open graph.png.

Here is the promised script (slight improvements from the original).

#!/usr/bin/env ruby

require "config/environment"

Dir.glob("app/models/*rb") { |f| require f }puts "digraph x {"

Dir.glob("app/models/*rb") do |f|

classname = f.match(//([a-z_]+).rb/)[1].camelize

klass = Kernel.const_get classname

if ActiveRecord::Base >= klass

puts classname

klass.reflect_on_all_associations.each do |a|

label = (a.macro.to_s =~ /many/) ? "*" : "1"

puts "#{classname} -> #{} [label=\"#{label}\"]"




puts "}"

And here is an example of the result:

Shooting Yourself in the Foot, Different ways

Run BunnyThere are basically two ways to shoot yourself in the foot these days:

Anybody willing to develop a peer-to-peer extension for Windows Media Player out there? I bet that would be a lot of fun.

Weather, Did You Say Weather?

The excellent scientific climate blog RealClimate has put up a small guide for beginners, with tons of links to good resources for those wanting to know a bit more. So the next time someone tells you “There ain’t no global warming, it’s just the sun heating up a bit more!” at a dinner, you’ll have some understanding of the problem and arguments to fight back.

Spread the word and do your homework.

Slashing File.join in Ruby

If you’re doing some file system manipulations in Ruby, moving and creating files around (which it does very well as it doesn’t try to ignore, like Java, that there is a file system) you have basically two choices: hard code a lot of ‘/’ characters in your code, which isn’t pretty, or use File.join. After a while you end up with many of these joins all over the place and it’s not so pretty (there’s actually a third possibility, you can directly use File::SEPARATOR, but it’s so ugly that I’m not counting it).

Yesterday, I had this really simple idea:

class String
def /(str_to_join)
File.join(self, str_to_join)

It’s so sweet and easy that I can’t even figure why I’ve never thought of it before. Now I can do:

'tmp'/'readme.txt' # literals
ROOT_DIR/file_path # variables holding strings

Much easier on the eyes and the fingers.


Seen on the ruby-talk mailing list, in answer to a guy who is asking how Ruby can be 100% object oriented when primitives like ‘if’ and ‘then’ are not objects:

> Seriously, what kind of object would an If object be? What is the
> essence of If-ness? I think if anybody could answer that question they
> would instantly achieve enlightment and then their head would explode.

That, my friends, is exactly how LISP programmers are created.

Which reminds me of this xkcd comic.

End of the Lisp bashing.

Linux Isn’t Quite There Yet

Just 3 snapshots. Tell me, which one still looks like in the eighties




Granted, you’ll see something much better if you check Ubuntu or Red Hat. But isn’t Linux, as an OS, supposed to go mainstream at some point?

“Cover Your Ass” Security

I usually don’t link much outside of my mains posts but Bruce Schneier, a security expert, wrote an interesting post about the “Cover Your Ass” security measures. Very effective to keep cops in shape. A pretty good blog by the way,