Thursday Night

Paul Betts’s personal website / blog / what-have-you

Two Cool Ruby packages: R-Bus and ActiveResource

View Comments

I came across two Ruby packages today that got me interested today. Every time I go on RubyForge, I find something cool.

Ruby finally gets on the Bus

Until recently, there has been no way for Ruby to use D-Bus; the bindings someone had previously written were badly out-of-date. Fortunately though, Kristoffer Lundén took up the challenge and wrote a decent binding called R-Bus. This means that Ruby can use all of the system services, from receiving hardware notifications to changing the media track in Rhythmbox.

ActiveResource – like ActiveRecord for Web APIs

I was always wondering why using web APIs in Ruby wasn’t as easy as it could be, but 37Signals fixed that right up: I tried out the Highrise API and it’s great. Dump your entire Highrise contacts in human-readable format in two lines (assuming you use the irb call described in the Highrise API docs:

require ‘yaml’
Highrise::Person.each { |x| puts x.to_yaml }

It doesn’t get any better than that. I’m pretty sure if Ruby fixes its runtime problems (like green threads + blocking IO) it will be the best programming language on the planet, at least in terms of lack of developer pain.

Written by Paul Betts

May 14th, 2007 at 9:55 pm

Posted in Ruby

Silverlight + Ruby for .NET? Awesome.

View Comments

As part of the big hullabaloo around Mix ’07, Microsoft announced their new Silverlight product, which is basically a less cracked-out Flash, which lets you use .NET languages (including IronPython) to create Flash-like animations and games. Finally, people can design awesome UIs and use a programming language that isn’t evil.

But more importantly, MS created a compiler for Ruby that compiles to MSIL called IronRuby. This means I can use Ruby at work and actually have other people use it. It also means that I might even learn this Silverlight thing if I can create awesome UIs in Ruby, and have it be cross-platform (even though “cross-platform” apparently doesn’t include Linux, don’t worry; Miguel’s gonna have that shit covered post-haste).

Written by Paul Betts

May 2nd, 2007 at 10:05 am

DHH Arrogant? Maybe. But make sure to catch his point

View Comments

There’s been a lot of news lately regarding Alex Payne of Twitter’s interview regarding Ruby on Rails and Twitter’s performance problems, and more about the creator of Ruby on Rails’ response. I read it and found it quite thoughtful though slightly “holier-than-thou”, but it seems like everyone now is hating on DHH. I think people missed the point, and it’s a huge one, so here it is:

If you don’t like something in open-source, just change it.

I think that what we’re really seeing is people not used to the concept of being able to take someone else’s work and change it. You see it with Wikipedia all the time, people will be like, “Well, I went to read a wp article, but it had a lot of the facts wrong”. My response? “So fix it!” It’s not that these people are lazy, or that they should be somehow expected to contribute, but they’ve simply got the corporate, “take it as-is” mindset, I find myself doing it too. The last couple years or so have been a subtle sea-change in information – the concept that you can change it. Especially in languages like Ruby, where you can (for better or worse) change almost everything in the language. DHH is saying this too, here’s the quote:

Second, when you work with open source and you discover new requirements not met by the software, it’s your shining opportunity to give something back. Rather than just sit around idle waiting for some vendor to fix your problems, you get the unique chance of being a steward of your own destiny. To become a participant in the community rather than a mere spectator. This is especially true with frameworks like Rails that are implemented in high-level languages like Ruby. The barriers to contribution are exceptionally low.

There’s a 45% chance he’s just being a tool, but take the good part out; once you get used to the fact that you can change stuff, a lot of things become a lot easier because you don’t have to “work-around” anything anymore.

Written by Paul Betts

April 17th, 2007 at 4:24 pm

Posted in Linux,Philosophy,Ruby

Amazon ads?

View Comments

I’m thinking about adding Amazon links to books that I think are awesome (not just random books). Thoughts? Comments? This is lame? For example, here’s a really awesome book on Ruby:


This book is pretty great

It’s an introduction to Ruby in the beginning, but has a lot of useful random routines, such as how to do numerical integration. This book is worth it solely for its chapters on Regular Expressions and dynamic programming (changing objects’ methods at runtime, etc). Basically whenever I want to go do something new with Ruby, I check this book first and it probably has some way to do it that makes me think Ruby is even more awesome.

So that’s it. And turns out if any of you buy this book after clicking there, I get some money. But don’t do it for me, do it so you’ll realize how awesome the Ruby language is.

Written by Paul Betts

March 13th, 2007 at 5:17 pm

Posted in Ruby

Autotorrent 0.5.1 – This one is actually useful

View Comments

After reading the feedback on kde-apps.org as well as discovering how awesome KTorrent is, I’ve added some features to Autotorrent that should make it much more useful for people. First, I added support for other torrent programs, and I was able to make the KTorrent support really nice using DCOP. Basically, it means that once you have a default directory set up, Autotorrent can silently add torrents without popping up the “OMG DO YOU WANT TO SAVE THIS?!” dialog.


This dialog isn’t very pretty but that’s alright

Next, after learning some basic QtRuby, I managed to make a decent configuration dialog. It’s not very well spaced and it was a giant pain-in-the-ass to get what I wanted done, probably because I’m used to the GTK+ way of doing things, particularly when it comes to signals and slots. I’m totally biased, but I think the Qt way just involves a lot of extra declarations and isn’t nearly as Ruby-like as Ruby-GNOME2

Update: PS here’s where you can download Autotorrent. Oops.

Written by Paul Betts

February 24th, 2007 at 2:38 pm

Posted in Linux,QT/KDE,Ruby

My foray into Amarok scripts: Autotorrent

View Comments

Recently, I’ve been listening to last.fm Internet radio a lot, it’s the first internet radio that I’ve found that both doesn’t sound like crap, and actually plays songs I like (actually, the coolest feature is “Neighbor radio”, which uses the songs I listen to on my computer to figure out what songs I’d like, and it does a pretty good job!). I wanted a way to download all the songs I liked automatically. First, I tried to rig Amarok to download the songs for me, but last.fm responded to that by halting the current song, then playing the song from Snakes on a Plane. Touché, last.fm. Touché.

After suffering through Cobra Starship’s massive crapfest, I decided to go an alternate route, and thus was born Autotorrent. Basically, it adds a new menu item on the right-click menu called “Download this album”. Autotorrent goes out to mininova.org, tracks down the album, tries to make sure it has the right track, then sends the Magnet URI to Azureus, which downloads the album.

This program may be slightly immoral, but I’m not too worried about it; the fact is, it’s not doing anything a regular person wouldn’t do anyways, it just automates it a bit. It also allows me to continue my part in the “Annoy big media companies” campaign. Boo hoo, now the music execs will only be able to afford 6 new Porsches this year after screwing real musicians out of every dime they deserve. Seriously, if you like a band, go to their show and buy stuff because that’s the only way that your money goes to where it belongs.

Of course, the script is in Ruby, what other language would make downloading webpages and searching through the text so easy? Well ok, Perl would, but I wanted something I could read in a week. Amarok scripts are really easy to write, if you have a cool idea that involves Amarok you should try to write it up because it’s not that hard to do. A handy tip is that if you’re looking for which functions you can call, if you run DCOP with parameters missing (ie dcop amarok), you’ll get a list of things you can use for the next parameter; in this case, it’d be ‘player’, ‘playlist’, etc…

Really, I wanted to write this program because I find that now that most of my friends are far away, I don’t really listen to new music anymore. I always wondered how people my parents’ age got stuck in a certain era of music; now I know. Luckily, the Internet will come to my rescue and help me out by discovering cool new music for me (to an extent, anyways). I also need to start playing guitar again, I haven’t played guitar regularly for almost a year now and that’s really lame, as well starting to record again and playing my banjo.

Update: Added screenshot to show the geezers that I’m not lightweight

Update 2: Evidently this never actually got posted, it got marked as private? Well now it’s here, tada!

Written by Paul Betts

February 19th, 2007 at 10:37 am

Posted in Linux,Ruby

New Program: Merge Thunderbird / Evolution style (mbox) folders

View Comments

To keep my desktop and my laptop in sync with each other, I use a program called Unison, and it works great for almost everything I throw at it. It’s fast, smart, and has the right amount of control and options to do exactly what I want, except for when it comes to one thing: mail.

Unfortunately, most of the time Unison’s diff-style merge doesn’t work for mbox mail files, which is what both Evolution and Mozilla SeaMonkey / Thunderbird (as well as a lot of other Email clients) use. It’s about impossible to merge two mail folders by hand if you have even more than 50 or so messages, and I didn’t know any way to do it using existing tools. So to fix this, I wrote a small command-line Ruby program that knows how to merge two mail folders without duplicates. Once I finished though, I had no idea if my program wasn’t losing mail messages in the merge, so I wrote another program that displays the differences between two mail folders in terms of messages.

Some examples:

# Merge three mailboxes
mboxmerge.exe Inbox_A Inbox_B Inbox_C > New_Inbox

# Show the differences between two mailboxes
mboxdiff.exe Inbox_A Inbox_B

# Merge all mailboxes that begin with the letter C
cat `find ~/mailboxes | grep ‘^C’` | mboxmerge.rb > C_Mailboxes

That last one was getting a little bit fancy, but you get the idea. The program uses the From, Subject, To, the Date, and the message text to determine if a message is unique, so it’s pretty thorough at telling which messages are identical between the mail files, so it’s safe to use it for synchronization purposes (which is why I wrote it anyways!)

Now, I use this on my own files and I’m reasonably sure that it won’t chew up your mail, but please make sure that you have a backup in case things go wrong or my program randomly freaks out and deletes everything. It will never write to its input files, so there’s no chance of that failing but the standard disclaimer applies.

Update: Also check out the script to automate syncing the local and remote copies of a mail folder

Written by Paul Betts

February 15th, 2007 at 10:26 pm

Posted in Linux,Microsoft,Ruby

How did I not know Ruby Facets existed?

View Comments

I just found out about Ruby Facets, a library of extensions to the Ruby Standard Library that has some really Computer Science’y things, like coroutines and lazy evaluation, as well as a lot of useful routines like deep copies and method caching (memoize).

This is the reason I like Ruby the most, because it brings back the theory and ideas to programming. I had feared it had been all but lost once people discovered, “Hey, I can write some crappy Java program and get paid tons of money to do it!”, then everything around computer science became centered around the cash. I suppose this is to be expected, since anything that has a lot of money around it gets corrupted, but it’s awesome to see something be both successful and interested in advancing the way that people write programs.

I guess in general, this highlights one of my personal flaws; that I really don’t understand money. Not the difference between a stock and a bond or some nonsense like that, but the fundamental concept of money. If one has enough money to do what they want, what is money actually worth to them? Being motivated to do something solely for money shows ‘a poverty of ambition’, as a up-and-coming presidential nominee/senator once said. I’m nervous about starting a career because of this, because quite frankly, I don’t really care about my company making money; I certainly don’t mind it, but it’s really tertiary to me and I have a feeling the further I get in a corporation, the more I’ll be pressured to put the stockholders as my #1 over making something better for people. Creating something great and seeing something that I make help people’s lives unfortunately doesn’t always line up with making a ton of money. So really, the moral of the story is:

  1. Helping people is worth something
  2. If you want to know what God thinks about money, just look at the people He gives it to. – Old Irish saying

Written by Paul Betts

February 8th, 2007 at 3:46 pm

Posted in Philosophy,Ruby

Why Ruby is so much shorter

View Comments

Chuck:
What features or aspects of Ruby enable it to be half the number of lines as Boo?

Well, there are several aspects of Ruby that allow it to be smaller. Firstly, I’m a better functional coder than I was when I wrote the Boo version, so some of it is certainly because of that. However, several aspects of Ruby make the code much more elegant than it was; I’ll highlight a few in this blog entry.

Using yield effectively

If you judiciously use yield to retrieve extra information from the caller, you can make your code read much better. For example, here’s an excerpt from the main() of my program

# Process the library
library.load file_list do |progress|
    puts _("%s%% complete") % (progress * 100.0)
end

library.find_soundtracks do |curname|
    print _("’%s’ may be a soundtrack or compilation. Is it? (Y/n) ") % curname
    (STDIN.gets =~ /^[Nn]/ ? false : true)
end

list = library.create_action_list(results[:target],
                  results[:musicformat],
                  results[:sndtrkformat]) do |tag, invalid|
    puts _("The tag ‘%s’ has invalid characters; ‘%s’ are not allowed") % [tag, invalid]
    puts _("Please enter a replacement:")
    STDIN.gets
end

# …. snip -> in find_soundtracks …

# Call out the handler
@is_soundtrack[curname] = true if yield(curname)

See how clean the main function reads? This kind of thing is really easy and clean in Ruby; in Boo I can do the same thing using delegates but there’s more syntactic nonsense (and even more in C#).

Regular Expressions are built into everything

Regular expression support exists in Boo and it’s better than C#, but Ruby really emphasizes using them in the standard library, whereas in .NET it feels kind of like an add-on. For example, here’s a (inefficient) method to pull all the XML’ish tags from a string (like <artist>/<album> => [“artist”, “album”…)

First_Tag = /(< \S*?>)+[^< ]*/
def get_tags(xml_string)
    # Find the tags used in the string
    s = xml_string.downcase
    tags_used = []
    while First_Tag.match s do
        tags_used << $1
        s.sub! First_Tag, ”
    end
        tags_used

I’m sure as soon as I post this, someone will chime up with a “That’s a horrible way to do that!”, but my point is, Regular Expressions being built-in to a lot of methods as well as all of the match parts (\1, \2, \3…) being set to global variables like Perl makes life much easier

Dynamic class loading ain’t no thang

In Ruby, it’s even easier than C# or Boo to load new classes, because the “using” statement (“require”) isn’t some special declaration, it’s a statement just like any other. This sample loads all the classes that can dig information out of music files:


def load_taggers(*paths)
    # Load the defaults, then the ones specified in the param
    paths.each do |x|
        Pathname.new(x).each_entry { |y| require File.join(x,y) if y.extname == '.rb' }
    end

    # Now reflect through the objects and find ones that match
    @taggers = []
    tagger_classes = ObjectSpace.each_object(Class) do |x|
        pm = x.public_instance_methods(true)
        next unless pm.include? 'get_tags?' and pm.include? 'song_info'
        @taggers < < x.instance
    end
    @taggers = nil unless @taggers.size > 0
end

Basically in this sample, we load all files in a list of directories, then we dig through all the classes looking for ones that implement the get_tags? and song_info methods. If so, we add the singleton instance to an array (All tag-retrieving classes are Singletons).

Right now, I only have a dummy implementation and one that uses TagLib, but I intend to add a few more to pick up WMA/M4A's, and so all I have to do to implement that is add two more files in those directories.

Written by Paul Betts

January 17th, 2007 at 1:09 pm

Posted in Mono / .NET,Ruby

What I’m working on right now #1: Estelle

View Comments

Right now I’m working on two programs for the open source world; one should be pretty handy and the other has the potential to be downright cool; here’s the first one:

Estelle – The music organizer

This is a program that will find all of your music files and put them under one folder organized in any way you choose. Why reinvent the wheel? Because frankly, all of the programs that do this seem to suck. They don’t give you enough control over how they organize, and if it doesn’t work right you’re stuck with a big mess; some of them also don’t handle soundtracks and compilations very well. Estelle has an option to create links instead of copy, so that you can have a working preview of the changes before you commit them, as well as treating soundtracks separately.

I’ve been writing this program for about a year and a half now on-and-off, mainly because I write a lot of it, get tired of it, then rewrite it in another language. Over winter break I rewrote the entire program in Ruby (including my own ghetto TagLib extension library), and it’s working better than it ever has. Right now I’m working on a GTK+ user interface, and I’ll also most likely make it an Amarok script as well. The code is only in SVN right now, the command to check out is:

svn co svn://paulbetts.org/Estelle-ruby

After rewriting my application again, I find in general that it’s just easier to get stuff done in Ruby than it is in C# and Boo. Of course, it depends on the problem and how much library support you have but all in all, Ruby makes application programming less of a pain.

Read on to see some line comparisons between C#, Boo, and Ruby:
Read the rest of this entry »

Written by Paul Betts

January 15th, 2007 at 7:25 pm

Posted in Linux,Ruby