Monday, November 19, 2007

My first Ruby gem

This past weekend has a special importance for me. I am finally able to stand by my favorite language, Ruby, with more than just words.

To cut a long story short, I'm playing with smart cards for my research. I didn't find any good library for accessing cards from Ruby, so I was faced with either writing my own extension, or leaving my favorite language for something else. My desire for beauty triumphed over my laziness, and my first Ruby extension was born.

It wasn't as bad as I expected. It took me 4 half-days to figure out PC/SC, ruby's extension mechanism, gems, and Rubyforge. And in the fifth day I rested got it to work under Linux/Windows.

So, yay, I've made my small contribution to the Ruby community. If you're curious to check it out, go to its Rubyforge page.

Wednesday, November 7, 2007

Resetting a Blackberry, All the Way

Why should you read this?
You have a Java (read: past 2 years) Blackberry. You want to start from a clean slate, and you realize that Wipe Handheld only goes half way there. You want to take advantage of the hours I spent googling this issue, and trust me it's non-trivial.

My Motivation (skip if you don't care)
My beloved Pearl started acting up, and I wanted to start from a clean slate. I did some unconventional experiments, so I was pretty sure that starting fresh would bring an advantage to the table.

I searched blackberry reset and firmware reflash and what not, and got no answer. In the end, I got bored and clicked some random links (I believe they had to do with hacking your Berry), and got to what I wanted.

So I'd like to share what I learned. Hopefully, Google will like the fact that I'm using its blogging tool and index the page with a good rank, so other people don't waste as much time as I did.

Proposed Solution
Without further ado, here's how I reset my Blackberry. It worked for me. Comments with refinements to my method are welcome.
  1. Backup your data
    I can't help here, because I didn't want to backup my data. Everything is synced with my Mac, so why risk restoring the cause of my issues?

  2. Wipe your handheld
    I'm not sure this is necessary, but I did it, and it can't hurt. Go to the Settings application, then get to Security > General Settings. Push the menu key (the one with the Blackberry logo) and select (yes, you've guessed it right!) Wipe Handheld from the menu. Let it do its thing for a few minutes.

  3. Get the tool to wipe your handheld for real
    The tool is pretty geeky (text-mode), but does the job. It's a wrapper around an even more geeky tool (javaloader from the Blackberry SDK). Here's a link to a place where you can get it from.
    Alternately, you can google JL_cmder and you should be able to find it.

  4. Wipe your Berry for real
    Connect your Blackberry to your PC via the USB cable. Exit Desktop Manager if it pops up. Then run the tool, choose wipe, and follow the on-screen pointers.
    Warning: those people aren't kididing. Pay attention on this step, so you don't brick your Berry.

  5. Get the firmware you want to install
    Go to to be pointed to the download site for your carrier. Or google the firmware you want. Google search works well for this part, so there's no need for me to post links. Install both the desktop software (Desktop Manager) and the firmware (Blackberry calls it handheld software).

  6. Install the new firmware
    Run Desktop Manager, while your Berry is still connected to the PC. Application Loader should pop up by itself, and let you choose which components of the firmware you want to install. I skipped the foreign languages, as well as applications that I don't use, and was happy to get the free space.
    Be patient. This can take anywhere from 30 minutes to an hour. Don't mess with the phone during this time, unless you have the time to go through a bricked phone replacement.
  7. Restore your data
    If you did a backup in step 1, now's the time to restore it. If not, you'll probably spend a couple of hours downloading your applications and redoing the phone's settings.

That's it. Enjoy, and share any refinements you come up with!

Monday, October 15, 2007

Java -- love / hate relationship

My standing is very simple and clear: I hate Java. I think it's an oversimplified language that takes away power of expression just so that it's accessible to idiots. And more idiots coding means more crappy applications / libraries / designs that I have to live with.

I know how Java started. It's scary to think of it, but I was already able to understand programming things around me. I think the binary-level portability idea is cool, and producing verifiable software is interesting, even though useless until someone solves the halting problem. However, Java apps (think Eclipse) aren't portable across OS versions, and variants, let alone processor types!! And don't get me started on J2ME -- write once, test on every phone model. So what we ended up getting is pretty much the same deployment we had with C++, except the language is idiot-proof at the expense of being much less powerful.

So, I don't think Java should exist. High-performance components should be written in C++, and non-CPU intensive work should be moved to ruby. Of course, this leaves out the poor retards who can't understand either (note that I'm asking for more than the ability to crank out code in a language) . And by God, they should stay out. Software teams will be more productive with fewer people that can actually code.

Ideals aside, I use Java. I use it because I have to, in class projects -- everyone knows Java, so classes ask for Java code in order to avoid having to teach people another language. I also use it because sometimes it's the easiest way to get the job done:
* phones only take user applications in Java form
* the only reasonable way of getting my code running on a smart card is the JavaCard platform
* the quickest way to get coding on Windows is installing Eclipse with its Java tools

When I start coding something, I'm thinking "it'd be really easy to do on ruby if I had this and that". But _this and that_ aren't there yet, and it'd take a lot of time to do them. And, despite my wish to make a contribution to the community, I don't have the time, because it's all supposed to be done yesterday. So all I do is start coding Java, and hope JRuby happens sooner rather than later.

Yay, obvious stuff top to bottom. Why do I bother to write this? I'm in an info-session, and I'm kindof bored.

Thursday, September 27, 2007

Miro: offline Youtube application for Mac

I've been looking for a YouTube front-end application forever, for three reasons:
  • the browser isn't integrated with my keyboard's Play / Stop buttons and my apple remote
  • YouTube history sucks, as it gets wiped every session
  • I want to watch all those illegal music videos wonderful user-generated movies offline
Here's what I found so far:
video player

It does concurrent downloads, and tags the unseen videos. This is convenient because I can get multiple variants of the same video, and see / delete the bad dupes. Unfortunately, it doesn't meet my main request -- no integration with the keyboard or apple remote.

Hope this helps people looking for something similar! Please comment if you find something better.

Happy watching,

Wednesday, September 26, 2007

BSD Sockets in Ruby

I love Ruby. Completely. I don't want to write in anything else. However, meeting with the BSD sockets interface left me with a sour taste. Here's what I learned. Hopefully, you'll find this post and won't waste time if you have to work with Ruby's sockets.

You can send packets with send
The method name is good for consistency, but a horrible choice in general, if you ask me, because it overrides send in Object (used to send a message to an object, same thing as calling a method).

send's parameters are a string representing the data (same encoding as in recv), and a number for flags. The number is NOT optional (messed up choice, in my opinion), so my sockets code always has send data, 0 in it.

send fails oddly
If you're seeeing in `send': symbol string may not contain `\0' (ArgumentError) then your socket is object is null. This happens because nil uses Object's send, so it thinks you're passing it a method name.

pack_sockaddr_in doesn't like localhost
Silly me, I looked the example using and I thought I can give pack_sockaddr_in any address. If I give it localhost, I get in `connect': Invalid argument - connect(2) (Errno::EINVAL).
Solution: use

Using strings for send / recv is annoying
If I'm using sockets, I'm working at the byte level. Using Fixnum arrays to represent bytes in a packet is a lot more convenient than using strings. Here are snippers for converting between the two formats. (I did Scheme at MIT, so I love map-reduce... I meant, collect-inject).

bytes > string:
block_string = { |value| value.chr }.join('')

string > bytes:
block = (0...block_string.length).map { |i| block_string[i] }

That's about it. Hope this helps someone!